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
7 * Use, modification, and distribution are subject to the terms specified in the
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);
19 msg = command + " is not on any key";
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:")));
29 function help_document_generator (document, buffer) {
30 dom_generator.call(this, document, XHTML_NS);
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");
40 parent.appendChild(node);
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) {
55 yield ref.open_in_editor();
57 handle_interactive_error(buffer.window, e);
59 event.preventDefault();
60 event.stopPropagation();
61 }, false /* capture */);
62 x.textContent = (module_name != null ? module_name : ref.file_name);
65 parent.appendChild(f);
69 command_name : function (name, parent) {
70 var node = this.element("span", "class", "command");
71 this.text(name, node);
73 parent.appendChild(node);
77 command_reference : function (name, parent) {
78 var node = this.element("a",
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);
90 parent.appendChild(node);
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);
99 parent.appendChild(node);
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) {
108 if (para.length == 0)
111 var p = this.element("p", f);
113 var regexp = /`([a-zA-Z0-9_\-$]+)\'/g;
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;
124 if (last_index < para.length)
125 this.text(para.substring(last_index), p);
128 parent.appendChild(f);
132 add_help_stylesheet : function () {
133 this.add_stylesheet("chrome://conkeror-gui/content/help.css");
138 function help_buffer_modality (buffer, element) {
139 buffer.keymaps.push(help_buffer_keymap);
146 define_keywords("$binding_list");
147 function describe_bindings_buffer (window, element) {
148 this.constructor_begin();
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 || "";
171 if (name in list_by_keymap)
172 km = list_by_keymap[name];
174 km = list_by_keymap[name] = {list_by_category: {}, category_list: [], name: name};
175 keymap_list.push(km);
177 let catname = x.category || "";
179 if (catname in km.list_by_category)
180 cat = km.list_by_category[catname];
182 cat = km.list_by_category[catname] = [];
185 km.category_list.unshift(cat);
187 km.category_list.push(cat);
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) {
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) {
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");
211 if (bind.command != null) {
212 if (typeof(bind.command) == "function") {
213 g.text("[function]", command_td);
215 g.text(bind.command, command_td);
216 let cmd = interactive_commands.get(bind.command);
218 help_str = cmd.shortdoc;
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);
229 __proto__: special_buffer.prototype
233 function describe_bindings (buffer, target) {
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)
242 for (let i = binding_stack.length - 1; i >= 0; --i) {
243 bound_in = binding_stack[i].bound_in;
245 if (bound_in.name && !bound_in.anonymous)
247 bound_in = bound_in.bound_in;
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
258 create_buffer(buffer.window, buffer_creator(describe_bindings_buffer,
260 $binding_list = list),
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));
278 define_keywords("$command_list");
279 function apropos_command_buffer (window, element) {
280 this.constructor_begin();
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);
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);
324 d.body.appendChild(table);
327 __proto__: special_buffer.prototype
331 /* TODO: support regexps/etc. */
332 function apropos_command (buffer, substring, target) {
334 interactive_commands.for_each(function (name, cmd) {
335 if (name.indexOf(substring) != -1) {
336 var binding = {name: name, cmd: cmd};
340 list.sort(function (a,b) {
347 create_buffer(buffer.window, buffer_creator(apropos_command_buffer,
349 $command_list = list),
353 function apropos_command_new_buffer (I) {
354 apropos_command(I.buffer,
355 (yield I.minibuffer.read($prompt = "Apropos command:",
356 $history = "apropos")),
359 function apropos_command_new_window (I) {
360 apropos_command(I.buffer,
361 (yield I.minibuffer.read($prompt = "Apropos command:",
362 $history = "apropos")),
365 interactive("apropos-command", "List commands whose names contain a given substring.",
366 alternates(apropos_command_new_buffer, apropos_command_new_window));
374 define_keywords("$command", "$bindings");
375 function describe_command_buffer (window, element) {
376 this.constructor_begin();
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");
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);
410 g.text(" is an interactive command.", p);
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) {
419 g.key_binding(this.bindings[i], p);
425 g.help_text(cmd.doc, d.body);
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,
439 $bindings = bindings),
442 function describe_command_new_buffer (I) {
443 describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
446 function describe_command_new_window (I) {
447 describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
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);});
468 define_keywords("$binding", "$other_bindings", "$key_sequence");
469 function describe_key_buffer (window, element) {
470 this.constructor_begin();
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");
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;
501 g.command_name("[pass through]", p);
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);
513 g.text(this.bind.browser_object, p);
516 if (this.source_code_reference) {
518 g.source_code_reference(this.source_code_reference, 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);
531 g.text(" is an interactive command.", p);
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) {
540 g.key_binding(this.bindings[i], p);
546 g.help_text(cmd.doc, d.body);
550 __proto__: special_buffer.prototype
554 function describe_key (buffer, key_info, target) {
556 var seq = key_info[0];
557 var bind = key_info[1];
558 var keymaps = get_current_keymaps(buffer.window);
560 bindings = keymap_lookup_command(keymaps, bind.command);
563 create_buffer(buffer.window,
564 buffer_creator(describe_key_buffer,
566 $key_sequence = seq.join(" "),
567 $other_bindings = bindings,
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)),
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)),
582 function describe_key_briefly (buffer, key_info) {
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+'"';
596 browser_object += bind.browser_object;
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,
607 describe_key_briefly(
609 (yield I.minibuffer.read_key_binding($prompt = "Describe key:", $buffer = I.buffer)));
618 define_keywords("$variable");
619 function describe_variable_buffer (window, element) {
620 this.constructor_begin();
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)
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);
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");
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);
671 g.text(" is a user variable.", p);
674 p = g.element("p", d.body);
675 g.text("Its value is: ", p);
676 let value = conkeror[this.variable];
678 let s = pretty_print_value(value);
679 let pre = g.element("pre", p);
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);
692 let s = pretty_print_value(uvar.default_value);
693 let pre = g.element("pre", p);
699 __proto__: special_buffer.prototype
703 function describe_variable (buffer, variable, target) {
704 create_buffer(buffer.window,
705 buffer_creator(describe_variable_buffer,
707 $variable = variable),
710 function describe_variable_new_buffer (I) {
711 describe_variable(I.buffer,
712 (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
715 function describe_variable_new_window (I) {
716 describe_variable(I.buffer,
717 (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
720 interactive("describe-variable", null,
721 alternates(describe_variable_new_buffer, describe_variable_new_window));
726 * Describe Preference
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:")),
738 function describe_preference_new_window (I) {
739 describe_preference(I.buffer, (yield I.minibuffer.read_preference($prompt = "Describe preference:")),
742 interactive("describe-preference", null,
743 alternates(describe_preference_new_buffer, describe_preference_new_window));