whitespace
[conkeror.git] / modules / labels.js
blobaa2c2197231edac3754bbbbfd6254c4c4c05b7a1
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 in_module(null);
10 require("pretty-print.js");
12 /**
13  * @param name specifies the name of the label to be defined
14  *
15  * The remaining arguments should be strings, specifying the names of
16  * required arguments. Additionally, the last additional argument can
17  * be `null', which indicates that unlimited optional arguments can
18  * follow. If `null' is not specified as the last argument, then it is
19  * assumed that all optional arguments are keyword arguments. The
20  * optional arguments are stored as an array in a member called $.
21  */
22 function define_label (name) {
23     var allow_optional = false;
24     var required_args = [];
25     for (let i = 1, ii = arguments.length; i < ii; ++i) {
26         if (arguments[i] === null) {
27             allow_optional = true;
28             if (i + 1 != ii)
29                 throw new Error("null must be the last argument");
30         } else {
31             required_args.push(arguments[i]);
32         }
33     }
34     function toString () {
35         let optional = this.$;
36         if (optional == null)
37             optional = [];
38         let printed_args = [];
39         let seen_defined_yet = false;
40         for (let i = required_args.length - 1; i >= 0; --i) {
41             let arg = required_args[i];
42             if (seen_defined_yet || this[arg] !== undefined) {
43                 printed_args.unshift(arg + " = " + pretty_print_value(this[arg]));
44                 seen_defined_yet = true;
45             }
46         }
47         printed_args.push.apply(null, optional.map(pretty_print_value));
48         for (let i in this) {
49             if (i.length > 1 && i[0] == "$")
50                 printed_args.push(i + " = " + this[i]);
51         }
52         if (printed_args.length > 0)
53             printed_args = "(" + printed_args.join(", ") + ")";
54         else
55             printed_args = "";
56         return this._name + printed_args;
57     }
58     var result;
59     result = function () {
60         var o = { _name: name, toString: toString, _id: result, __is_label: true, toSource: toString };
61         let max_req_arg = arguments.length;
62         if (max_req_arg > required_args.length)
63             max_req_arg = required_args.length;
64         for (let i = 0; i < max_req_arg; ++i)
65             o[required_args[i]] = arguments[i];
66         if (allow_optional)
67             o.$ = Array.prototype.slice.call(arguments, required_args.length);
68         else
69             write_keywords(o, arguments, required_args.length);
70         return o;
71     };
72     result._name = name;
73     result._id = result;
74     result.$ = [];
75     for (let i in required_args)
76         result[i] = undefined;
77     result.toString = toString;
78     result.__is_label = true;
79     conkeror[name] = result;
82 function label_id (value) {
83     if (value == null)
84         return null;
85     return value._id;
88 provide("labels");