]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/highlight.jsm
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / modules / highlight.jsm
index 35288942f54c24afb5b97cfa1e8b09a65fee63da..fca93478b3f7ce514cabba374e700eaf5bde62a8 100644 (file)
@@ -1,15 +1,16 @@
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2008-2012 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";
 
-Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("highlight", {
     exports: ["Highlight", "Highlights", "highlight"],
-    require: ["services", "styles", "util"],
-    use: ["messages", "template"]
-}, this);
+    require: ["services", "util"]
+});
+
+lazyRequire("styles", ["Styles", "styles"]);
+lazyRequire("template", ["template"]);
 
 var Highlight = Struct("class", "selector", "sites",
                        "defaultExtends", "defaultValue",
@@ -33,6 +34,8 @@ Highlight.liveProperty = function (name, prop) {
                     h.style.css = h.css;
 
         this.style[prop || name] = this[prop || name];
+        if (this.onChange)
+            this.onChange();
     });
 }
 Highlight.liveProperty("agent");
@@ -100,7 +103,7 @@ var Highlights = Module("Highlight", {
     __iterator__: function () values(this.highlight).sort(function (a, b) String.localeCompare(a.class, b.class))
                                                     .iterValues(),
 
-    _create: function (agent, args) {
+    _create: function _create(agent, args) {
         let obj = Highlight.apply(Highlight, args);
 
         if (!isArray(obj.sites))
@@ -143,14 +146,14 @@ var Highlights = Module("Highlight", {
         return obj;
     },
 
-    get: function (k) this.highlight[k],
+    get: function get(k) this.highlight[k],
 
-    set: function (key, newStyle, force, append, extend) {
+    set: function set(key, newStyle, force, append, extend) {
         let [, class_, selectors] = key.match(/^([a-zA-Z_-]+)(.*)/);
 
         let highlight = this.highlight[key] || this._create(false, [key]);
 
-        let bases = extend || highlight.extend;
+        let bases = extend || highlight.extends;
         if (append) {
             newStyle = Styles.append(highlight.value || "", newStyle);
             bases = highlight.extends.concat(bases);
@@ -181,7 +184,7 @@ var Highlights = Module("Highlight", {
      * Clears all highlighting rules. Rules with default values are
      * reset.
      */
-    clear: function () {
+    clear: function clear() {
         for (let [k, v] in Iterator(this.highlight))
             this.set(k, null, true);
     },
@@ -193,8 +196,8 @@ var Highlights = Module("Highlight", {
      * @param {Node} node
      * @param {string} group
      */
-    highlightNode: function (node, group, applyBindings) {
-        node.setAttributeNS(NS.uri, "highlight", group);
+    highlightNode: function highlightNode(node, group, applyBindings) {
+        node.setAttributeNS(NS, "highlight", group);
 
         let groups = group.split(" ");
         for each (let group in groups)
@@ -214,21 +217,22 @@ var Highlights = Module("Highlight", {
      *
      * @param {string} class
      */
-    selector: function (class_)
-        let (self = this)
-           class_.replace(/(^|[>\s])([A-Z][\w-]+)\b/g,
-            function (m, n1, hl) n1 +
-                (self.highlight[hl] && self.highlight[hl].class != class_
-                    ? self.highlight[hl].selector : "[dactyl|highlight~=" + hl + "]")),
-
-    groupRegexp: util.regexp(<![CDATA[
+    selector: function selector(class_)
+        class_.replace(/(^|[>\s])([A-Z][\w-]+)\b/g,
+            (m, n1, hl) => {
+                if (this.highlight[hl] && this.highlight[hl].class != class_)
+                    return n1 + this.highlight[hl].selector;
+                return n1 + "[dactyl|highlight~=" + hl + "]";
+            }),
+
+    groupRegexp: util.regexp(literal(/*
         ^
         (\s* (?:\S|\s\S)+ \s+)
         \{ ([^}]*) \}
         \s*
         $
-    ]]>, "gmx"),
-    sheetRegexp: util.regexp(<![CDATA[
+    */), "gmx"),
+    sheetRegexp: util.regexp(literal(/*
         ^\s*
         !? \*?
              (?P<group>    (?:[^;\s]|\s[^;\s])+ )
@@ -237,7 +241,8 @@ var Highlights = Module("Highlight", {
         (?:; (?P<extends>  (?:[^;\s]|\s[^;\s])+ )? )?
         \s*  (?P<css>      .*)
         $
-    ]]>, "x"),
+    */), "x"),
+    // </css>
 
     /**
      * Bulk loads new CSS rules, in the format of,
@@ -275,8 +280,9 @@ var Highlights = Module("Highlight", {
      * @param {string} css The rules to load. See {@link Highlights#css}.
      * @param {boolean} eager When true, load all provided rules immediately.
      */
-    loadCSS: function (css, eager) {
-        String.replace(css, this.groupRegexp, function (m, m1, m2) m1 + " " + m2.replace(/\n\s*/g, " "))
+    loadCSS: function loadCSS(css, eager) {
+        String.replace(css, /\\\n/g, "")
+              .replace(this.groupRegexp, function (m, m1, m2) m1 + " " + m2.replace(/\n\s*/g, " "))
               .split("\n").filter(function (s) /\S/.test(s) && !/^\s*\/\//.test(s))
               .forEach(function (highlight) {
 
@@ -291,7 +297,7 @@ var Highlights = Module("Highlight", {
     }
 }, {
 }, {
-    commands: function (dactyl, modules) {
+    commands: function initCommands(dactyl, modules) {
         const { autocommands, commands, completion, CommandOption, config, io } = modules;
 
         let lastScheme;
@@ -318,7 +324,7 @@ var Highlights = Module("Highlight", {
         commands.add(["hi[ghlight]"],
             "Set the style of certain display elements",
             function (args) {
-                let style = <![CDATA[
+                let style = literal(/*
                     ;
                     display: inline-block !important;
                     position: static !important;
@@ -326,7 +332,7 @@ var Highlights = Module("Highlight", {
                     width: 3em !important; min-width: 3em !important; max-width: 3em !important;
                     height: 1em !important; min-height: 1em !important; max-height: 1em !important;
                     overflow: hidden !important;
-                ]]>;
+                */);
                 let clear = args[0] == "clear";
                 if (clear)
                     args.shift();
@@ -337,7 +343,7 @@ var Highlights = Module("Highlight", {
                 if (!modify && /&$/.test(key))
                     [clear, modify, key] = [true, true, key.replace(/&$/, "")];
 
-                dactyl.assert(!(clear && css), _("error.trailing"));
+                dactyl.assert(!(clear && css), _("error.trailingCharacters"));
 
                 if (!modify)
                     modules.commandline.commandOutput(
@@ -345,11 +351,13 @@ var Highlights = Module("Highlight", {
                             ["padding: 0 1em 0 0; vertical-align: top; max-width: 16em; overflow: hidden;",
                              "text-align: center"],
                             ([h.class,
-                              <span style={"text-align: center; line-height: 1em;" + h.value + style}>XXX</span>,
-                              template.map(h.extends, template.highlight),
-                              template.highlightRegexp(h.value, /\b[-\w]+(?=:)/g)]
-                                for (h in highlight)
-                                if (!key || h.class.indexOf(key) > -1))));
+                              ["span", { style: "text-align: center; line-height: 1em;" + h.value + style }, "XXX"],
+                              template.map(h.extends, function (s) template.highlight(s), ","),
+                              template.highlightRegexp(h.value, /\b[-\w]+(?=:)|\/\*.*?\*\//g,
+                                                       function (match) ["span", { highlight: match[0] == "/" ? "Comment" : "Key" }, match])
+                             ]
+                             for (h in highlight)
+                             if (!key || h.class.indexOf(key) > -1))));
                 else if (!key && clear)
                     highlight.clear();
                 else if (key)
@@ -369,7 +377,10 @@ var Highlights = Module("Highlight", {
                     else if (args.completeArg == 1) {
                         let hl = highlight.get(args[0]);
                         if (hl)
-                            context.completions = [[hl.value, "Current Value"], [hl.defaultValue || "", "Default Value"]];
+                            context.completions = [
+                                [hl.value, _("option.currentValue")],
+                                [hl.defaultValue || "", _("option.defaultValue")]
+                            ];
                         context.fork("css", 0, completion, "css");
                     }
                 },
@@ -385,8 +396,8 @@ var Highlights = Module("Highlight", {
                             let group = args[0] && highlight.get(args[0]);
                             if (group)
                                 context.fork("extra", 0, this, function (context) [
-                                     [String(group.extends), "Current Value"],
-                                     [String(group.defaultExtends) || "", "Default Value"]
+                                     [String(group.extends), _("option.currentValue")],
+                                     [String(group.defaultExtends) || "", _("option.defaultValue")]
                                 ]);
                             context.fork("groups", 0, completion, "highlightGroup");
                         }
@@ -396,24 +407,32 @@ var Highlights = Module("Highlight", {
                     {
                         command: this.name,
                         arguments: [v.class],
-                        literalArg: v.value
+                        literalArg: v.value,
+                        options: {
+                            "-link": v.extends.length ? v.extends : undefined
+                        }
                     }
                     for (v in Iterator(highlight))
                     if (v.value != v.defaultValue)
                 ]
             });
     },
-    completion: function (dactyl, modules) {
+    completion: function initCompletion(dactyl, modules) {
         const { completion, config, io } = modules;
+
         completion.colorScheme = function colorScheme(context) {
             let extRe = RegExp("\\." + config.fileExtension + "$");
 
             context.title = ["Color Scheme", "Runtime Path"];
             context.keys = { text: function (f) f.leafName.replace(extRe, ""), description: ".parent.path" };
-            context.completions = array.flatten(
-                io.getRuntimeDirectories("colors").map(
-                    function (dir) dir.readDirectory().filter(
-                        function (file) extRe.test(file.leafName))));
+            context.completions =
+                array.flatten(
+                        io.getRuntimeDirectories("colors").map(
+                            function (dir) dir.readDirectory().filter(
+                                function (file) extRe.test(file.leafName))))
+                     .concat([
+                        { leafName: "default", parent: { path: /*L*/"Revert to builtin colorscheme" } }
+                     ]);
 
         };
 
@@ -422,7 +441,7 @@ var Highlights = Module("Highlight", {
             context.completions = [[v.class, v.value] for (v in highlight)];
         };
     },
-    javascript: function (dactyl, modules, window) {
+    javascript: function initJavascript(dactyl, modules, window) {
         modules.JavaScript.setCompleter(["get", "set"].map(function (m) highlight[m]),
             [ function (context, obj, args) Iterator(highlight.highlight) ]);
         modules.JavaScript.setCompleter(["highlightNode"].map(function (m) highlight[m]),
@@ -434,4 +453,4 @@ var Highlights = Module("Highlight", {
 
 endModule();
 
-// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: