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
13 require("special-buffer.js");
14 require("interactive.js");
16 function where_is_command (buffer, command) {
17 var keymaps = get_current_keymaps(buffer.window);
18 var list = keymap_lookup_command(keymaps, command);
21 msg = command + " is not on any key";
23 msg = command + " is on " + list.join(", ");
24 buffer.window.minibuffer.message(msg);
26 interactive("where-is", null, function (I) {
27 where_is_command(I.buffer,
28 (yield I.minibuffer.read_command($prompt = "Where is command:")));
31 function help_document_generator (document, buffer) {
32 dom_generator.call(this, document, XHTML_NS);
35 help_document_generator.prototype = {
36 constructor: help_document_generator,
37 __proto__: dom_generator.prototype,
39 key_binding: function (str, parent) {
40 var node = this.element("span", "class", "key-binding");
43 parent.appendChild(node);
47 source_code_reference: function (ref, parent) {
48 var f = this.document.createDocumentFragment();
49 var module_name = ref.module_name;
50 var buffer = this.buffer;
51 //f.appendChild(this.text(module_name != null ? "module " : "file "));
52 var x = this.element("a",
53 "class", "source-code-reference",
54 "href", "javascript:");
55 x.addEventListener("click", function (event) {
58 yield ref.open_in_editor();
60 handle_interactive_error(buffer.window, e);
62 event.preventDefault();
63 event.stopPropagation();
64 }, false /* capture */);
65 x.textContent = (module_name != null ? module_name : ref.file_name);
68 parent.appendChild(f);
72 command_name: function (name, parent) {
73 var node = this.element("span", "class", "command");
74 this.text(name, node);
76 parent.appendChild(node);
80 command_reference: function (name, parent) {
81 var node = this.element("a",
83 "href", "javascript:");
84 var buffer = this.buffer;
85 node.addEventListener("click", function (event) {
86 /* FIXME: don't hardcode browse target */
87 describe_command(buffer, name, OPEN_NEW_BUFFER);
88 event.preventDefault();
89 event.stopPropagation();
90 }, false /* capture */);
91 this.text(name, node);
93 parent.appendChild(node);
97 variable_reference: function (name, parent) {
98 var node = this.element("a", "class", "variable", "href", "#");
99 /* FIXME: make this work */
100 this.text(name, node);
102 parent.appendChild(node);
106 help_text: function (str, parent) {
107 var paras = str.split("\n");
108 var f = this.document.createDocumentFragment();
109 for (var i = 0; i < paras.length; ++i) {
111 if (para.length == 0)
114 var p = this.element("p", f);
116 var regexp = /`([a-zA-Z0-9_\-$]+)\'/g;
120 while ((match = regexp.exec(para)) != null) {
121 this.text(para.substring(last_index, match.index), p);
122 var command = match[1];
123 /* FIXME: check if it is a valid command */
124 this.command_reference(command, p);
125 last_index = regexp.lastIndex;
127 if (last_index < para.length)
128 this.text(para.substring(last_index), p);
131 parent.appendChild(f);
135 add_help_stylesheet: function () {
136 this.add_stylesheet("chrome://conkeror-gui/content/help.css");
141 function help_buffer_modality (buffer, element) {
142 buffer.keymaps.push(help_buffer_keymap);
149 define_keywords("$binding_list");
150 function describe_bindings_buffer (window) {
151 this.constructor_begin();
153 special_buffer.call(this, window, forward_keywords(arguments));
154 this.binding_list = arguments.$binding_list;
155 this.modalities.push(help_buffer_modality);
156 this.constructor_end();
158 describe_bindings_buffer.prototype = {
159 constructor: describe_bindings_buffer,
160 title: "Key bindings",
162 description: "*bindings*",
164 generate: function () {
165 var d = this.document;
166 var list = this.binding_list;
167 delete this.binding_list;
169 var list_by_keymap = {};
170 var keymap_list = [];
171 for each (let x in list) {
172 let name = x.bound_in || "";
174 if (name in list_by_keymap)
175 km = list_by_keymap[name];
177 km = list_by_keymap[name] = {list_by_category: {}, category_list: [], name: name};
178 keymap_list.push(km);
180 let catname = x.category || "";
182 if (catname in km.list_by_category)
183 cat = km.list_by_category[catname];
185 cat = km.list_by_category[catname] = [];
188 km.category_list.unshift(cat);
190 km.category_list.push(cat);
195 var g = new help_document_generator(d, this);
196 g.add_help_stylesheet();
198 d.body.setAttribute("class", "help-list");
200 for each (let km in keymap_list) {
201 g.text(km.name, g.element("h1", d.body));
202 for each (let cat in km.category_list) {
204 g.text(cat.name, g.element("h2", d.body));
206 let table = g.element("table", d.body);
207 for (var i = 0; i < cat.length; ++i) {
209 let tr = g.element("tr", table, "class", (i % 2 == 0) ? "even" : "odd");
210 let seq_td = g.element("td", tr, "class", "key-binding");
211 g.text(bind.seq, seq_td);
212 let command_td = g.element("td", tr, "class", "command");
214 if (bind.command != null) {
215 if (typeof(bind.command) == "function") {
216 g.text("[function]", command_td);
218 let cmd = interactive_commands.get(bind.command);
220 g.command_reference(cmd.name, command_td);
221 help_str = cmd.shortdoc;
223 g.text(bind.command, command_td);
226 } else if (bind.keymap != null) {
227 g.text("["+bind.keymap+"]", command_td);
228 } else if (bind.fallthrough)
229 g.text("[pass through]", command_td);
230 let help_td = g.element("td", tr, "class", "help");
231 g.text(help_str || "", help_td);
237 __proto__: special_buffer.prototype
241 function describe_bindings (buffer, target, keymaps, prefix) {
244 keymaps = get_current_keymaps(buffer.window);
246 prefix = format_binding_sequence(
247 prefix.map(function (x) { return {key:x}; }))+" ";
250 for_each_key_binding(keymaps, function (binding_stack) {
251 var last = binding_stack[binding_stack.length - 1];
252 //we don't care about auto-generated keymap bindings.
253 if (last.keymap && last.keymap.anonymous)
257 for (let i = binding_stack.length - 1; i >= 0; --i) {
258 bound_in = binding_stack[i].bound_in;
260 if (bound_in.name && !bound_in.anonymous)
262 bound_in = bound_in.bound_in;
266 if (last.keymap && ! last.keymap.anonymous)
267 keymap = last.keymap.name;
268 var bind = {seq: prefix+format_binding_sequence(binding_stack),
269 fallthrough: last.fallthrough,
270 command: last.command,
272 bound_in: bound_in.name,
273 category: last.category
277 create_buffer(buffer.window, buffer_creator(describe_bindings_buffer,
279 $binding_list = list),
282 function describe_bindings_new_buffer (I) {
283 describe_bindings(I.buffer, OPEN_NEW_BUFFER);
285 function describe_bindings_new_window (I) {
286 describe_bindings(I.buffer, OPEN_NEW_WINDOW);
288 interactive("describe-bindings",
289 "Show a help buffer describing the bindings in the context keymaps, "+
290 "meaning the top-level keymaps according to the focus context in the "+
292 alternates(describe_bindings_new_buffer,
293 describe_bindings_new_window));
295 function describe_active_bindings_new_buffer (I) {
296 describe_bindings(I.buffer, OPEN_NEW_BUFFER,
297 I.keymaps || get_current_keymaps(I.buffer.window),
298 I.key_sequence.slice(0, -1));
300 function describe_active_bindings_new_window (I) {
301 describe_bindings(I.buffer, OPEN_NEW_WINDOW,
302 I.keymaps || get_current_keymaps(I.buffer.window),
303 I.key_sequence.slice(0, -1));
305 interactive("describe-active-bindings",
306 "Show a help buffer describing the bindings in the active keymaps, "+
307 "meaning the keymaps in the middle of an ongoing key sequence. This "+
308 "command is intended to be called via `sequence_help_keymap'. For "+
309 "that reason, `describe-active-bindings' does not consume and prefix "+
310 "commands like `universal-argument', as doing so would lead to "+
311 "ambiguities with respect to the intent of the user.",
312 describe_active_bindings_new_buffer);
319 define_keywords("$command_list");
320 function apropos_command_buffer (window) {
321 this.constructor_begin();
323 special_buffer.call(this, window, forward_keywords(arguments));
324 this.command_list = arguments.$command_list;
325 this.modalities.push(help_buffer_modality);
326 this.constructor_end();
328 apropos_command_buffer.prototype = {
329 constructor: apropos_command_buffer,
330 title: "Apropos commands",
332 description: "*Apropos*",
334 generate: function () {
335 var d = this.document;
336 var list = this.command_list;
337 delete this.command_list;
339 var g = new help_document_generator(d, this);
340 g.add_help_stylesheet();
342 d.body.setAttribute("class", "help-list");
344 var table = d.createElementNS(XHTML_NS, "table");
345 for (var i = 0; i < list.length; ++i) {
346 var binding = list[i];
347 var tr = d.createElementNS(XHTML_NS, "tr");
348 tr.setAttribute("class", (i % 2 == 0) ? "even" : "odd");
350 var command_td = d.createElementNS(XHTML_NS,"td");
351 g.command_reference(binding.name, command_td);
354 if (binding.cmd.shortdoc != null)
355 shortdoc = binding.cmd.shortdoc;
356 tr.appendChild(command_td);
358 var shortdoc_td = d.createElementNS(XHTML_NS, "td");
359 shortdoc_td.setAttribute("class", "help");
360 shortdoc_td.textContent = shortdoc;
361 tr.appendChild(shortdoc_td);
363 table.appendChild(tr);
365 d.body.appendChild(table);
368 __proto__: special_buffer.prototype
372 /* TODO: support regexps/etc. */
373 function apropos_command (buffer, substring, target) {
375 interactive_commands.for_each(function (name, cmd) {
376 if (name.indexOf(substring) != -1) {
377 var binding = {name: name, cmd: cmd};
381 list.sort(function (a,b) {
388 create_buffer(buffer.window, buffer_creator(apropos_command_buffer,
390 $command_list = list),
394 function apropos_command_new_buffer (I) {
395 apropos_command(I.buffer,
396 (yield I.minibuffer.read($prompt = "Apropos command:",
397 $history = "apropos")),
400 function apropos_command_new_window (I) {
401 apropos_command(I.buffer,
402 (yield I.minibuffer.read($prompt = "Apropos command:",
403 $history = "apropos")),
406 interactive("apropos-command", "List commands whose names contain a given substring.",
407 alternates(apropos_command_new_buffer, apropos_command_new_window));
415 define_keywords("$command", "$bindings");
416 function describe_command_buffer (window) {
417 this.constructor_begin();
419 special_buffer.call(this, window, forward_keywords(arguments));
420 this.bindings = arguments.$bindings;
421 this.command = arguments.$command;
422 this.cmd = interactive_commands.get(this.command);
423 this.source_code_reference = this.cmd.source_code_reference;
424 this.modalities.push(help_buffer_modality);
425 this.constructor_end();
427 describe_command_buffer.prototype = {
428 constructor: describe_command_buffer,
429 get title () { return "Command help: " + this.command; },
431 description: "*help*",
433 generate: function () {
434 var d = this.document;
436 var g = new help_document_generator(d, this);
438 g.add_help_stylesheet();
439 d.body.setAttribute("class", "describe-command");
443 p = g.element("p", d.body);
444 g.command_reference(this.command, p);
445 var cmd = interactive_commands.get(this.command);
446 if (cmd.source_code_reference) {
447 g.text(" is an interactive command in ", p);
448 g.source_code_reference(cmd.source_code_reference, p);
451 g.text(" is an interactive command.", p);
454 if (this.bindings.length > 0) {
455 p = g.element("p", d.body);
456 g.text("It is bound to ", p);
457 for (var i = 0; i < this.bindings.length; ++i) {
460 g.key_binding(this.bindings[i], p);
466 g.help_text(cmd.doc, d.body);
469 __proto__: special_buffer.prototype
473 function describe_command (buffer, command, target) {
474 var keymaps = get_current_keymaps(buffer.window);
475 var bindings = keymap_lookup_command(keymaps, command);
476 create_buffer(buffer.window,
477 buffer_creator(describe_command_buffer,
480 $bindings = bindings),
483 function describe_command_new_buffer (I) {
484 describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
487 function describe_command_new_window (I) {
488 describe_command(I.buffer, (yield I.minibuffer.read_command($prompt = "Describe command:")),
491 interactive("describe-command", null,
492 alternates(describe_command_new_buffer, describe_command_new_window));
496 function view_referenced_source_code (buffer) {
497 if (buffer.source_code_reference == null)
498 throw interactive_error("Command not valid in current buffer.");
499 yield buffer.source_code_reference.open_in_editor();
501 interactive("view-referenced-source-code", null,
502 function (I) {yield view_referenced_source_code(I.buffer);});
509 define_keywords("$binding", "$other_bindings", "$key_sequence");
510 function describe_key_buffer (window) {
511 this.constructor_begin();
513 special_buffer.call(this, window, forward_keywords(arguments));
514 this.key_sequence = arguments.$key_sequence;
515 this.bindings = arguments.$other_bindings;
516 this.bind = arguments.$binding;
517 this.source_code_reference = this.bind.source_code_reference;
518 this.modalities.push(help_buffer_modality);
519 this.constructor_end();
521 describe_key_buffer.prototype = {
522 constructor: describe_key_buffer,
523 get title () { return "Key help: " + this.key_sequence; },
525 description: "*help*",
527 generate: function () {
528 var d = this.document;
530 var g = new help_document_generator(d, this);
532 g.add_help_stylesheet();
533 d.body.setAttribute("class", "describe-key");
537 p = g.element("p", d.body);
538 g.key_binding(this.key_sequence, p);
539 g.text(" is bound to the command ", p);
540 var command = this.bind.command;
542 g.command_name("[pass through]", p);
544 g.command_reference(command, p);
545 if (this.bind.browser_object != null) {
546 g.text(" with the browser object, ", p);
547 if (this.bind.browser_object instanceof Function) {
548 g.text("<anonymous browser-object function>", p);
549 } else if (this.bind.browser_object instanceof browser_object_class) {
550 g.text(this.bind.browser_object.name, p);
551 } else if (typeof(this.bind.browser_object) == "string") {
552 g.text('"'+this.bind.browser_object+'"', p);
554 g.text(this.bind.browser_object, p);
557 if (this.source_code_reference) {
559 g.source_code_reference(this.source_code_reference, p);
563 if (command != null) {
564 p = g.element("p", d.body);
565 g.command_reference(command, p);
566 var cmd = interactive_commands.get(command);
567 if (cmd.source_code_reference) {
568 g.text(" is an interactive command in ", p);
569 g.source_code_reference(cmd.source_code_reference, p);
572 g.text(" is an interactive command.", p);
575 if (this.bindings.length > 0) {
576 p = g.element("p", d.body);
577 g.text("It is bound to ", p);
578 for (var i = 0; i < this.bindings.length; ++i) {
581 g.key_binding(this.bindings[i], p);
587 g.help_text(cmd.doc, d.body);
591 __proto__: special_buffer.prototype
595 function describe_key (buffer, key_info, target) {
597 var seq = key_info[0];
598 var bind = key_info[1];
599 var keymaps = get_current_keymaps(buffer.window);
601 bindings = keymap_lookup_command(keymaps, bind.command);
604 create_buffer(buffer.window,
605 buffer_creator(describe_key_buffer,
607 $key_sequence = seq.join(" "),
608 $other_bindings = bindings,
612 function describe_key_new_buffer (I) {
613 describe_key(I.buffer,
614 (yield I.minibuffer.read_key_binding($prompt = "Describe key:")),
617 function describe_key_new_window (I) {
618 describe_key(I.buffer,
619 (yield I.minibuffer.read_key_binding($prompt = "Describe key:")),
623 function describe_key_briefly (buffer, key_info) {
625 var seq = key_info[0];
626 var bind = key_info[1];
627 var browser_object = "";
628 if (bind.browser_object != null) {
629 browser_object += " on the browser object, ";
630 if (bind.browser_object instanceof Function) {
631 browser_object += "<anonymous browser-object function>";
632 } else if (bind.browser_object instanceof browser_object_class) {
633 browser_object += bind.browser_object.name;
634 } else if (typeof(bind.browser_object) == "string") {
635 browser_object += '"'+bind.browser_object+'"';
637 browser_object += bind.browser_object;
640 buffer.window.minibuffer.message(seq.join(" ") + " runs the command " + bind.command + browser_object);
643 interactive("describe-key", null,
644 alternates(describe_key_new_buffer, describe_key_new_window));
646 interactive("describe-key-briefly", null,
648 describe_key_briefly(
650 (yield I.minibuffer.read_key_binding($prompt = "Describe key:")));
659 define_keywords("$variable");
660 function describe_variable_buffer (window) {
661 this.constructor_begin();
663 special_buffer.call(this, window, forward_keywords(arguments));
664 this.variable = arguments.$variable;
665 this.cmd = user_variables[this.variable];
666 this.source_code_reference = this.cmd.source_code_reference;
667 this.modalities.push(help_buffer_modality);
668 this.constructor_end();
670 describe_variable_buffer.prototype = {
671 constructor: describe_variable_buffer,
672 get title () { return "Variable help: " + this.variable; },
674 description: "*help*",
676 generate: function () {
677 var d = this.document;
679 var g = new help_document_generator(d, this);
681 g.add_help_stylesheet();
682 d.body.setAttribute("class", "describe-variable");
686 p = g.element("p", d.body);
687 g.variable_reference(this.variable, p);
688 var uvar = user_variables[this.variable];
689 if (uvar.source_code_reference) {
690 g.text(" is a user variable in ", p);
691 g.source_code_reference(uvar.source_code_reference, p);
694 g.text(" is a user variable.", p);
697 p = g.element("p", d.body);
698 g.text("Its value is: ", p);
699 let value = conkeror[this.variable];
701 let s = pretty_print_value(value);
702 let pre = g.element("pre", p);
706 if (uvar.doc != null)
707 g.help_text(uvar.doc, d.body);
709 if (uvar.default_value !== undefined &&
710 (uvar.default_value !== value ||
711 (typeof(uvar.default_value) != "object"))) {
712 p = g.element("p", d.body);
713 g.text("Its default value is: ", p);
715 let s = pretty_print_value(uvar.default_value);
716 let pre = g.element("pre", p);
722 __proto__: special_buffer.prototype
726 function describe_variable (buffer, variable, target) {
727 create_buffer(buffer.window,
728 buffer_creator(describe_variable_buffer,
730 $variable = variable),
733 function describe_variable_new_buffer (I) {
734 describe_variable(I.buffer,
735 (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
738 function describe_variable_new_window (I) {
739 describe_variable(I.buffer,
740 (yield I.minibuffer.read_user_variable($prompt = "Describe variable:")),
743 interactive("describe-variable", null,
744 alternates(describe_variable_new_buffer, describe_variable_new_window));
749 * Describe Preference
752 function describe_preference (buffer, preference, target) {
753 let key = preference.charAt(0).toUpperCase() + preference.substring(1);
754 let url = "http://kb.mozillazine.org/" + key;
755 browser_object_follow(buffer, target, url);
757 function describe_preference_new_buffer (I) {
758 describe_preference(I.buffer, (yield I.minibuffer.read_preference($prompt = "Describe preference:")),
761 function describe_preference_new_window (I) {
762 describe_preference(I.buffer, (yield I.minibuffer.read_preference($prompt = "Describe preference:")),
765 interactive("describe-preference", null,
766 alternates(describe_preference_new_buffer, describe_preference_new_window));