2 * (C) Copyright 2007-2009 John J. Foerch
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5 * Use, modification, and distribution are subject to the terms specified in the
11 var interactive_commands = new string_hashmap();
14 * name: string name of the command.
16 * doc: Documentation string, may be null.
18 * handler: A function to handle the command.
20 * The $prefix keyword, when true, means that the command
21 * is a prefix-command.
23 define_keywords("$prefix", "$browser_object", "$prompt");
24 function interactive (name, doc, handler) {
29 browser_object: arguments.$browser_object,
30 prefix: arguments.$prefix,
32 shortdoc: get_shortdoc_string(doc),
33 prompt: arguments.$prompt,
34 source_code_reference: get_caller_source_code_reference() };
36 interactive_commands.put(name, cmd);
40 function interactive_error (str) {
41 var e = new Error(str);
42 e.__proto__ = interactive_error.prototype;
45 interactive_error.prototype.__proto__ = Error.prototype;
48 function interactive_context (buffer) {
49 this.local = conkeror;
52 this.window = this.buffer.window;
54 this.local = buffer.page.local;
56 this.local = buffer.local;
60 interactive_context.prototype = {
62 get P () this.prefix_argument,
64 get p () univ_arg_to_number(this.prefix_argument),
66 set p (default_value) univ_arg_to_number(this.prefix_argument, default_value),
68 get minibuffer () this.window.minibuffer,
72 function handle_interactive_error (window, e) {
75 if (e instanceof interactive_error) {
76 window.minibuffer.message(e.message);
77 } else if (e instanceof abort) {
78 window.minibuffer.message("Quit");
81 window.minibuffer.message("call interactively: " + e);
85 function call_interactively (I, command) {
87 var window = I.window;
89 if (typeof command == "function") {
90 // Special interactive command
95 var cmd = interactive_commands.get(command);
97 handle_interactive_error(
99 interactive_error("Invalid command: " + command));
106 // if there was no interactive browser-object,
107 // binding_browser_object becomes the default.
108 if (I.browser_object == null) {
109 I.browser_object = I.binding_browser_object;
111 // if the command's default browser object is a non-null literal,
112 // it overrides an interactive browser-object, but not a binding
114 if (cmd.browser_object != null &&
115 (! (cmd.browser_object instanceof browser_object_class)) &&
116 (I.binding_browser_object == null))
118 I.browser_object = cmd.browser_object;
120 // if we still have no browser-object, look for a page-mode
121 // default, or finally the command default.
122 if (I.browser_object == null) {
124 (I.buffer && I.buffer.default_browser_object_classes[command]) ||
128 handler = cmd.handler;
131 while (typeof(handler) == "string") {
132 let parent = interactive_commands.get(handler);
133 handler = parent.handler;
134 if (handler == command) {
135 throw (interactive_error("circular command alias, "+command));
142 handle_interactive_error(window, e);
145 handle_interactive_error(window, e);
150 function alternates () {
151 let alts = Array.prototype.slice.call(arguments, 0);
152 return function (I) {
154 if (I.prefix_argument instanceof Array) {
155 let num = I.prefix_argument = I.prefix_argument[0];
156 while (num >= 4 && index + 1 < alts.length) {
161 yield alts[index](I);
167 * Utility functions for use in the rc to alter the behavior
168 * of interactive commands.
170 function set_handler (name, handler) {
171 var cmd = interactive_commands.get(name);
172 cmd.handler = handler;
175 function set_default_browser_object (name, browser_object) {
176 var cmd = interactive_commands.get(name);
177 cmd.browser_object = browser_object;