Prepare new Debian package
[conkeror.git] / modules / help.js
blobcd231efbcfcbed7e166360bbc94f27ab680c01f6
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nelson Elhage
4  * (C) Copyright 2008 David Glasser
5  * (C) Copyright 2009 John J. Foerch
6  *
7  * Use, modification, and distribution are subject to the terms specified in the
8  * COPYING file.
9 **/
11 require("special-buffer.js");
12 require("interactive.js");
14 function where_is_command (buffer, command) {
15     var keymaps = get_current_keymaps(buffer.window);
16     var list = keymap_lookup_command(keymaps, command);
17     var msg;
18     if (list.length == 0)
19         msg = command + " is not on any key";
20     else
21         msg = command + " is on " + list.join(", ");
22     buffer.window.minibuffer.message(msg);
24 interactive("where-is", null, function (I) {
25     where_is_command(I.buffer,
26                      (yield I.minibuffer.read_command($prompt = "Where is command:")));
27 });
29 function help_document_generator (document, buffer) {
30     dom_generator.call(this, document, XHTML_NS);
31     this.buffer = buffer;
33 help_document_generator.prototype = {
34     __proto__: dom_generator.prototype,
36     key_binding : function (str, parent) {
37         var node = this.element("span", "class", "key-binding");
38         this.text(str, node);
39         if (parent)
40             parent.appendChild(node);
41         return node;
42     },
44     source_code_reference : function (ref, parent) {
45         var f = this.document.createDocumentFragment();
46         var module_name = ref.module_name;
47         var buffer = this.buffer;
48         //f.appendChild(this.text(module_name != null ? "module " : "file "));
49         var x = this.element("a",
50                              "class", "source-code-reference",
51                              "href", "javascript:");
52         x.addEventListener("click", function (event) {
53             co_call(function () {
54                 try {
55                     yield ref.open_in_editor();
56                 } catch (e) {
57                     handle_interactive_error(buffer.window, e);
58                 }}());
59             event.preventDefault();
60             event.stopPropagation();
61         }, false /* capture */);
62         x.textContent = (module_name != null ? module_name : ref.file_name);
63         f.appendChild(x);
64         if (parent)
65             parent.appendChild(f);
66         return f;
67     },
69     command_name : function (name, parent) {
70         var node = this.element("span", "class", "command");
71         this.text(name, node);
72         if (parent)
73             parent.appendChild(node);
74         return node;
75     },
77     command_reference : function (name, parent) {
78         var node = this.element("a",
79                                 "class", "command",
80                                 "href", "javascript:");
81         var buffer = this.buffer;
82         node.addEventListener("click", function (event) {
83                                   /* FIXME: don't hardcode browse target */
84                                   describe_command(buffer, name, OPEN_NEW_BUFFER);
85                                   event.preventDefault();
86                                   event.stopPropagation();
87             }, false /* capture */);
88         this.text(name, node);
89         if (parent)
90             parent.appendChild(node);
91         return node;
92     },
94     variable_reference : function (name, parent) {
95         var node = this.element("a", "class", "variable", "href", "#");
96         /* FIXME: make this work */
97         this.text(name, node);
98         if (parent)
99             parent.appendChild(node);
100         return node;
101     },
103     help_text : function (str, parent) {
104         var paras = str.split("\n");
105         var f = this.document.createDocumentFragment();
106         for (var i = 0; i < paras.length; ++i) {
107             var para = paras[i];
108             if (para.length == 0)
109                 continue;
111             var p = this.element("p", f);
113             var regexp = /`([a-zA-Z0-9_\-$]+)\'/g;
115             var match;
116             var last_index = 0;
117             while ((match = regexp.exec(para)) != null) {
118                 this.text(para.substring(last_index, match.index), p);
119                 var command = match[1];
120                 /* FIXME: check if it is a valid command */
121                 this.command_reference(command, p);
122                 last_index = regexp.lastIndex;
123             }
124             if (last_index < para.length)
125                 this.text(para.substring(last_index), p);
126         }
127         if (parent != null)
128             parent.appendChild(f);
129         return f;
130     },
132     add_help_stylesheet : function () {
133         this.add_stylesheet("chrome://conkeror-gui/content/help.css");
134     }
138 function help_buffer_modality (buffer, element) {
139     buffer.keymaps.push(help_buffer_keymap);
143  * Describe Bindings
144  */
146 define_keywords("$binding_list");
147 function describe_bindings_buffer (window, element) {
148     this.constructor_begin();
149     keywords(arguments);
150     special_buffer.call(this, window, element, forward_keywords(arguments));
151     this.binding_list = arguments.$binding_list;
152     this.modalities.push(help_buffer_modality);
153     this.constructor_end();
156 describe_bindings_buffer.prototype = {
157     title : "Key bindings",
159     description : "*bindings*",
161     generate : function () {
162         var d = this.document;
163         var list = this.binding_list;
164         delete this.binding_list;
166         var list_by_keymap = {};
167         var keymap_list = [];
168         for each (let x in list) {
169             let name = x.bound_in || "";
170             let km;
171             if (name in list_by_keymap)
172                 km = list_by_keymap[name];
173             else {
174                 km = list_by_keymap[name] = {list_by_category: {}, category_list: [], name: name};
175                 keymap_list.push(km);
176             }
177             let catname = x.category || "";
178             let cat;
179             if (catname in km.list_by_category)
180                 cat = km.list_by_category[catname];
181             else {
182                 cat = km.list_by_category[catname] = [];
183                 cat.name = catname;
184                 if (catname == "")
185                     km.category_list.unshift(cat);
186                 else
187                     km.category_list.push(cat);
188             }
189             cat.push(x);
190         }
192         var g = new help_document_generator(d, this);
193         g.add_help_stylesheet();
195         d.body.setAttribute("class", "help-list");
197         for each (let km in keymap_list) {
198             g.text(km.name, g.element("h1", d.body));
199             for each (let cat in km.category_list) {
200                 if (cat.name != "")
201                     g.text(cat.name, g.element("h2", d.body));
203                 let table = g.element("table", d.body);
204                 for (var i = 0; i < cat.length; ++i) {
205                     let bind = cat[i];
206                     let tr = g.element("tr", table, "class", (i % 2 == 0) ? "even" : "odd");
207                     let seq_td = g.element("td", tr, "class", "key-binding");
208                     g.text(bind.seq, seq_td);
209                     let command_td = g.element("td", tr, "class", "command");
210                     let help_str = null;
211                     if (bind.command != null) {
212                         if (typeof(bind.command) == "function") {
213                             g.text("[function]", command_td);
214                         } else {
215                             g.text(bind.command, command_td);
216                             let cmd = interactive_commands.get(bind.command);
217                             if (cmd != null)
218                                 help_str = cmd.shortdoc;
219                         }
220                     } else if (bind.fallthrough)
221                         g.text("[pass through]", command_td);
222                     let help_td = g.element("td", tr, "class", "help");
223                     g.text(help_str || "", help_td);
224                 }
225             }
226         }
227     },
229     __proto__: special_buffer.prototype
233 function describe_bindings (buffer, target) {
234     var list = [];
235     var keymaps = get_current_keymaps(buffer.window);
236     for_each_key_binding(keymaps, function (binding_stack) {
237             var last = binding_stack[binding_stack.length - 1];
238             if (last.command == null && !last.fallthrough)
239                 return;
240             let bound_in = null;
241         outer:
242             for (let i = binding_stack.length - 1; i >= 0; --i) {
243                 bound_in = binding_stack[i].bound_in;
244                 while (bound_in) {
245                     if (bound_in.name && !bound_in.anonymous)
246                         break outer;
247                     bound_in = bound_in.bound_in;
248                 }
249             }
250             var bind = {seq: format_binding_sequence(binding_stack),
251                         fallthrough: last.fallthrough,
252                         command: last.command,
253                         bound_in: bound_in.name,
254                         category: last.category
255                        };
256             list.push(bind);
257         });
258     create_buffer(buffer.window, buffer_creator(describe_bindings_buffer,
259                                                 $opener = buffer,
260                                                 $binding_list = list),
261                   target);
263 function describe_bindings_new_buffer (I) {
264     describe_bindings(I.buffer, OPEN_NEW_BUFFER);
266 function describe_bindings_new_window (I) {
267     describe_bindings(I.buffer, OPEN_NEW_WINDOW);
269 interactive("describe-bindings", null,
270             alternates(describe_bindings_new_buffer, describe_bindings_new_window));
275  * Apropos Command
276  */
278 define_keywords("$command_list");
279 function apropos_command_buffer (window, element) {
280     this.constructor_begin();
281     keywords(arguments);
282     special_buffer.call(this, window, element, forward_keywords(arguments));
283     this.command_list = arguments.$command_list;
284     this.modalities.push(help_buffer_modality);
285     this.constructor_end();
288 apropos_command_buffer.prototype = {
289     title : "Apropos commands",
291     description : "*Apropos*",
293     generate : function () {
294         var d = this.document;
295         var list = this.command_list;
296         delete this.command_list;
298         var g = new help_document_generator(d, this);
299         g.add_help_stylesheet();
301         d.body.setAttribute("class", "help-list");
303         var table = d.createElementNS(XHTML_NS, "table");
304         for (var i = 0; i < list.length; ++i) {
305             var binding = list[i];
306             var tr = d.createElementNS(XHTML_NS, "tr");
307             tr.setAttribute("class", (i % 2 == 0) ? "even" : "odd");
309             var command_td = d.createElementNS(XHTML_NS,"td");
310             g.command_reference(binding.name, command_td);
312             var shortdoc = "";
313             if (binding.cmd.shortdoc != null)
314                 shortdoc = binding.cmd.shortdoc;
315             tr.appendChild(command_td);
317             var shortdoc_td = d.createElementNS(XHTML_NS, "td");
318             shortdoc_td.setAttribute("class", "help");
319             shortdoc_td.textContent = shortdoc;
320             tr.appendChild(shortdoc_td);
322             table.appendChild(tr);
323         }
324         d.body.appendChild(table);
325     },
327     __proto__: special_buffer.prototype
331 /* TODO: support regexps/etc. */
332 function apropos_command (buffer, substring, target) {
333     var list = [];
334     interactive_commands.for_each(function (name, cmd) {
335         if (name.indexOf(substring) != -1) {
336             var binding = {name: name, cmd: cmd};
337             list.push(binding);
338         }
339     });
340     list.sort(function (a,b) {
341                   if (a.name < b.name)
342                       return -1;
343                   if (a.name > b.name)
344                       return 1;
345                   return 0
346               });
347     create_buffer(buffer.window, buffer_creator(apropos_command_buffer,
348                                                 $opener = buffer,
349                                                 $command_list = list),
350                   target);
353 function apropos_command_new_buffer (I) {
354     apropos_command(I.buffer,
355                     (yield I.minibuffer.read($prompt = "Apropos command:",
356                                              $history = "apropos")),
357                     OPEN_NEW_BUFFER);
359 function apropos_command_new_window (I) {
360     apropos_command(I.buffer,
361                     (yield I.minibuffer.read($prompt = "Apropos command:",
362                                              $history = "apropos")),
363                     OPEN_NEW_WINDOW);
365 interactive("apropos-command", "List commands whose names contain a given substring.",
366             alternates(apropos_command_new_buffer, apropos_command_new_window));
371  * Describe Command
372  */
374 define_keywords("$command", "$bindings");
375 function describe_command_buffer (window, element) {
376     this.constructor_begin();
377     keywords(arguments);
378     special_buffer.call(this, window, element, forward_keywords(arguments));
379     this.bindings = arguments.$bindings;
380     this.command = arguments.$command;
381     this.cmd = interactive_commands.get(this.command);
382     this.source_code_reference = this.cmd.source_code_reference;
383     this.modalities.push(help_buffer_modality);
384     this.constructor_end();
387 describe_command_buffer.prototype = {
388     get title() { return "Command help: " + this.command; },
390     description : "*help*",
392     generate : function () {
393         var d = this.document;
395         var g = new help_document_generator(d, this);
397         g.add_help_stylesheet();
398         d.body.setAttribute("class", "describe-command");
400         var p;
402         p = g.element("p", d.body);
403         g.command_reference(this.command, p);
404         var cmd = interactive_commands.get(this.command);
405         if (cmd.source_code_reference)  {
406             g.text(" is an interactive command in ", p);
407             g.source_code_reference(cmd.source_code_reference, p);
408             g.text(".", p);
409         } else {
410             g.text(" is an interactive command.", p);
411         }
413         if (this.bindings.length > 0) {
414             p = g.element("p", d.body);
415             g.text("It is bound to ", p);
416             for (var i = 0; i < this.bindings.length; ++i) {
417                 if (i != 0)
418                     g.text(", ", p);
419                 g.key_binding(this.bindings[i], p);
420             }
421             g.text(".", p);
422         }
424         if (cmd.doc != null)
425             g.help_text(cmd.doc, d.body);
426     },
428     __proto__: special_buffer.prototype
432 function describe_command (buffer, command, target) {
433     var keymaps = get_current_keymaps(buffer.window);
434     var bindings = keymap_lookup_command(keymaps, command);
435     create_buffer(buffer.window,
436                   buffer_creator(describe_command_buffer,
437                                  $opener = buffer,
438                                  $command = command,
439                                  $bindings = bindings),
440                   target);
442 function describe_command_new_buffer (I) {
443     describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
444                      OPEN_NEW_BUFFER);
446 function describe_command_new_window (I) {
447     describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
448                      OPEN_NEW_WINDOW);
450 interactive("describe-command", null,
451             alternates(describe_command_new_buffer, describe_command_new_window));
455 function view_referenced_source_code (buffer) {
456     if (buffer.source_code_reference == null)
457         throw interactive_error("Command not valid in current buffer.");
458     yield buffer.source_code_reference.open_in_editor();
460 interactive("view-referenced-source-code", null,
461             function (I) {yield view_referenced_source_code(I.buffer);});
465  * Describe Key
466  */
468 define_keywords("$binding", "$other_bindings", "$key_sequence");
469 function describe_key_buffer (window, element) {
470     this.constructor_begin();
471     keywords(arguments);
472     special_buffer.call(this, window, element, forward_keywords(arguments));
473     this.key_sequence = arguments.$key_sequence;
474     this.bindings = arguments.$other_bindings;
475     this.bind = arguments.$binding;
476     this.source_code_reference = this.bind.source_code_reference;
477     this.modalities.push(help_buffer_modality);
478     this.constructor_end();
481 describe_key_buffer.prototype = {
482     get title() { return "Key help: " + this.key_sequence; },
484     description : "*help*",
486     generate : function () {
487         var d = this.document;
489         var g = new help_document_generator(d, this);
491         g.add_help_stylesheet();
492         d.body.setAttribute("class", "describe-key");
494         var p;
496         p = g.element("p", d.body);
497         g.key_binding(this.key_sequence, p);
498         g.text(" is bound to the command ", p);
499         var command = this.bind.command;
500         if (command == null)
501             g.command_name("[pass through]", p);
502         else
503             g.command_reference(command, p);
504         if (this.bind.browser_object != null) {
505             g.text(" with the browser object, ", p);
506             if (this.bind.browser_object instanceof Function) {
507                 g.text("<anonymous browser-object function>", p);
508             } else if (this.bind.browser_object instanceof browser_object_class) {
509                 g.text(this.bind.browser_object.name, p);
510             } else if (typeof(this.bind.browser_object) == "string") {
511                 g.text('"'+this.bind.browser_object+'"', p);
512             } else {
513                 g.text(this.bind.browser_object, p);
514             }
515         }
516         if (this.source_code_reference) {
517             g.text(" in ", p);
518             g.source_code_reference(this.source_code_reference, p);
519         }
520         g.text(".", p);
522         if (command != null) {
523             p = g.element("p", d.body);
524             g.command_reference(command, p);
525             var cmd = interactive_commands.get(command);
526             if (cmd.source_code_reference)  {
527                 g.text(" is an interactive command in ", p);
528                 g.source_code_reference(cmd.source_code_reference, p);
529                 g.text(".", p);
530             } else {
531                 g.text(" is an interactive command.", p);
532             }
534             if (this.bindings.length > 0) {
535                 p = g.element("p", d.body);
536                 g.text("It is bound to ", p);
537                 for (var i = 0; i < this.bindings.length; ++i) {
538                     if (i != 0)
539                         g.text(", ", p);
540                     g.key_binding(this.bindings[i], p);
541                 }
542                 g.text(".", p);
543             }
545             if (cmd.doc != null)
546                 g.help_text(cmd.doc, d.body);
547         }
548     },
550     __proto__: special_buffer.prototype
554 function describe_key (buffer, key_info, target) {
555     var bindings;
556     var seq = key_info[0];
557     var bind = key_info[1];
558     var keymaps = get_current_keymaps(buffer.window);
559     if (bind.command)
560         bindings = keymap_lookup_command(keymaps, bind.command);
561     else
562         bindings = [];
563     create_buffer(buffer.window,
564                   buffer_creator(describe_key_buffer,
565                                  $opener = buffer,
566                                  $key_sequence = seq.join(" "),
567                                  $other_bindings = bindings,
568                                  $binding = bind),
569                   target);
571 function describe_key_new_buffer (I) {
572     describe_key(I.buffer,
573                  (yield I.minibuffer.read_key_binding($prompt = "Describe key:", $buffer = I.buffer)),
574                  OPEN_NEW_BUFFER);
576 function describe_key_new_window (I) {
577     describe_key(I.buffer,
578                  (yield I.minibuffer.read_key_binding($prompt = "Describe key:", $buffer = I.buffer)),
579                  OPEN_NEW_WINDOW);
582 function describe_key_briefly (buffer, key_info) {
583     var bindings;
584     var seq = key_info[0];
585     var bind = key_info[1];
586     var browser_object = "";
587     if (bind.browser_object != null) {
588         browser_object += " on the browser object, ";
589         if (bind.browser_object instanceof Function) {
590             browser_object += "<anonymous browser-object function>";
591         } else if (bind.browser_object instanceof browser_object_class) {
592             browser_object += bind.browser_object.name;
593         } else if (typeof(bind.browser_object) == "string") {
594             browser_object += '"'+bind.browser_object+'"';
595         } else {
596             browser_object += bind.browser_object;
597         }
598     }
599     buffer.window.minibuffer.message(seq.join(" ") + " runs the command " + bind.command + browser_object);
602 interactive("describe-key", null,
603             alternates(describe_key_new_buffer, describe_key_new_window));
605 interactive("describe-key-briefly", null,
606     function (I) {
607         describe_key_briefly(
608             I.buffer,
609             (yield I.minibuffer.read_key_binding($prompt = "Describe key:", $buffer = I.buffer)));
610     });
615  * Describe Variable
616  */
618 define_keywords("$variable");
619 function describe_variable_buffer (window, element) {
620     this.constructor_begin();
621     keywords(arguments);
622     special_buffer.call(this, window, element, forward_keywords(arguments));
623     this.variable = arguments.$variable;
624     this.cmd = user_variables[this.variable];
625     this.source_code_reference = this.cmd.source_code_reference;
626     this.modalities.push(help_buffer_modality);
627     this.constructor_end();
630 function pretty_print_value (value) {
631     if (value === undefined)
632         return "undefined";
633     if (value === null)
634         return "null";
635     if (typeof(value) == "object")
636         return value.toSource();
637     if (typeof(value) == "function")
638         return value.toString();
639     if (typeof(value) == "string") {
640         let s = value.toSource();
641         // toSource returns: (new String("<blah>"))
642         // we want just: "<blah>"
643         return s.substring(12, s.length - 2);
644     }
645     return new String(value);
648 describe_variable_buffer.prototype = {
649     get title() { return "Variable help: " + this.variable; },
651     description : "*help*",
653     generate : function () {
654         var d = this.document;
656         var g = new help_document_generator(d, this);
658         g.add_help_stylesheet();
659         d.body.setAttribute("class", "describe-variable");
661         var p;
663         p = g.element("p", d.body);
664         g.variable_reference(this.variable, p);
665         var uvar = user_variables[this.variable];
666         if (uvar.source_code_reference)  {
667             g.text(" is a user variable in ", p);
668             g.source_code_reference(uvar.source_code_reference, p);
669             g.text(".", p);
670         } else {
671             g.text(" is a user variable.", p);
672         }
674         p = g.element("p", d.body);
675         g.text("Its value is: ", p);
676         let value = conkeror[this.variable];
677         {
678             let s = pretty_print_value(value);
679             let pre = g.element("pre", p);
680             g.text(s, pre);
681         }
683         if (uvar.doc != null)
684             g.help_text(uvar.doc, d.body);
686         if (uvar.default_value !== undefined &&
687             (uvar.default_value !== value ||
688              (typeof(uvar.default_value) != "object")))  {
689             p = g.element("p", d.body);
690             g.text("Its default value is: ", p);
691             {
692                 let s = pretty_print_value(uvar.default_value);
693                 let pre = g.element("pre", p);
694                 g.text(s, pre);
695             }
696         }
697     },
699     __proto__: special_buffer.prototype
703 function describe_variable (buffer, variable, target) {
704     create_buffer(buffer.window,
705                   buffer_creator(describe_variable_buffer,
706                                  $opener = buffer,
707                                  $variable = variable),
708                   target);
710 function describe_variable_new_buffer (I) {
711     describe_variable(I.buffer,
712                       (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
713                       OPEN_NEW_BUFFER);
715 function describe_variable_new_window (I) {
716     describe_variable(I.buffer,
717                       (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
718                       OPEN_NEW_WINDOW);
720 interactive("describe-variable", null,
721             alternates(describe_variable_new_buffer, describe_variable_new_window));
726  * Describe Preference
727  */
729 function describe_preference (buffer, preference, target) {
730     let key = preference.charAt(0).toUpperCase() + preference.substring(1);
731     let url = "http://kb.mozillazine.org/" + key;
732     browser_object_follow(buffer, target, url);
734 function describe_preference_new_buffer (I) {
735     describe_preference(I.buffer, (yield I.minibuffer.read_preference($prompt = "Describe preference:")),
736                         OPEN_NEW_BUFFER);
738 function describe_preference_new_window (I) {
739     describe_preference(I.buffer, (yield I.minibuffer.read_preference($prompt = "Describe preference:")),
740                         OPEN_NEW_WINDOW);
742 interactive("describe-preference", null,
743             alternates(describe_preference_new_buffer, describe_preference_new_window));