2 * (C) Copyright 2007-2008 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) {
51 this.window = this.buffer.window;
54 interactive_context.prototype = {
56 get P () this.prefix_argument,
58 get p () univ_arg_to_number(this.prefix_argument),
60 set p (default_value) univ_arg_to_number(this.prefix_argument, default_value),
62 get minibuffer () this.window.minibuffer,
64 get : function (x) this.buffer.get(x)
68 function handle_interactive_error (window, e) {
71 if (e instanceof interactive_error) {
72 window.minibuffer.message(e.message);
73 } else if (e instanceof abort) {
74 window.minibuffer.message("Quit");
77 window.minibuffer.message("call interactively: " + e);
81 function call_interactively (I, command) {
84 var window = I.window;
86 if (typeof(command) == "function") {
87 // Special interactive command
92 var cmd = interactive_commands.get(command);
94 handle_interactive_error(
96 interactive_error("Invalid command: " + command));
102 // if the default is a non-null literal, it always overrides.
103 if (cmd.browser_object != null &&
104 (! (cmd.browser_object instanceof browser_object_class)))
106 I.browser_object = cmd.browser_object;
109 // if no browser-object was specified interactively, use a
110 // default, possibly specified by the page-mode.
111 if (I.browser_object == null) {
113 (I.buffer && I.buffer.default_browser_object_classes[command]) ||
117 handler = cmd.handler;
120 while (typeof(handler) == "string") {
121 let parent = interactive_commands.get(handler);
122 handler = parent.handler;
123 if (handler == command) {
124 throw (interactive_error("circular command alias, "+command));
128 var result = handler(I);
129 if (is_coroutine(result)) {
134 handle_interactive_error(window, e);
140 handle_interactive_error(window, e);
145 function alternates () {
146 let alts = Array.prototype.slice.call(arguments, 0);
147 return function (I) {
149 if (I.prefix_argument instanceof Array) {
150 let num = I.prefix_argument = I.prefix_argument[0];
151 while (num >= 4 && index + 1 < alts.length) {
156 yield alts[index](I);
162 * Utility functions for use in the rc to alter the behavior
163 * of interactive commands.
165 function set_handler (name, handler) {
166 var cmd = interactive_commands.get(name);
167 cmd.handler = handler;
170 function set_default_browser_object (name, browser_object) {
171 var cmd = interactive_commands.get(name);
172 cmd.browser_object = browser_object;