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
13 var interactive_commands = new string_hashmap();
16 * name: string name of the command.
18 * doc: Documentation string, may be null.
20 * handler: A function to handle the command.
22 * The $prefix keyword, when true, means that the command
23 * is a prefix-command.
25 define_keywords("$prefix", "$browser_object", "$prompt");
26 function interactive (name, doc, handler) {
31 browser_object: arguments.$browser_object,
32 prefix: arguments.$prefix,
34 shortdoc: get_shortdoc_string(doc),
35 prompt: arguments.$prompt,
36 source_code_reference: get_caller_source_code_reference() };
38 interactive_commands.put(name, cmd);
42 function interactive_error (str) {
43 var e = new Error(str);
44 e.__proto__ = interactive_error.prototype;
47 interactive_error.prototype.__proto__ = Error.prototype;
50 function interactive_context (buffer) {
51 this.local = conkeror;
54 this.window = this.buffer.window;
56 this.local = buffer.page.local;
58 this.local = buffer.local;
62 interactive_context.prototype = {
63 constructor: interactive_context,
65 get P () this.prefix_argument,
67 get p () univ_arg_to_number(this.prefix_argument),
69 set p (default_value) univ_arg_to_number(this.prefix_argument, default_value),
71 get minibuffer () this.window.minibuffer,
75 function handle_interactive_error (window, e) {
78 if (e instanceof interactive_error) {
79 window.minibuffer.message(e.message);
80 } else if (e instanceof abort) {
81 window.minibuffer.message("Quit");
84 window.minibuffer.message("call interactively: " + e);
88 function call_interactively (I, command) {
90 var window = I.window;
92 if (typeof command == "function") {
93 // Special interactive command
98 var cmd = interactive_commands.get(command);
100 handle_interactive_error(
102 interactive_error("Invalid command: " + command));
107 handler = cmd.handler;
110 while (typeof handler == "string") {
111 let parent = interactive_commands.get(handler);
112 handler = parent.handler;
113 if (handler == command) {
114 throw (interactive_error("circular command alias, "+command));
121 handle_interactive_error(window, e);
124 handle_interactive_error(window, e);
129 function alternates () {
130 let alts = Array.prototype.slice.call(arguments, 0);
131 return function (I) {
133 if (I.prefix_argument instanceof Array) {
134 let num = I.prefix_argument = I.prefix_argument[0];
135 while (num >= 4 && index + 1 < alts.length) {
140 yield alts[index](I);
146 * Utility functions for use in the rc to alter the behavior
147 * of interactive commands.
149 function set_handler (name, handler) {
150 var cmd = interactive_commands.get(name);
151 cmd.handler = handler;
154 function set_default_browser_object (name, browser_object) {
155 var cmd = interactive_commands.get(name);
156 cmd.browser_object = browser_object;
159 provide("interactive");