interactive.js: fix alternates to properly propagate exceptions
[conkeror.git] / modules / interactive.js
blob3b0e4dc501f84f957b87ef0cd1620674f52ec6c4
1 /**
2  * (C) Copyright 2007-2008 John J. Foerch
3  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("utils.js");
11 var interactive_commands = new string_hashmap();
13 /**
14  * name: string name of the command.
15  *
16  * doc: Documentation string, may be null.
17  *
18  * handler: A function to handle the command.
19  *
20  * The $prefix keyword, when true, means that the command
21  * is a prefix-command.
22  */
23 define_keywords("$prefix", "$browser_object", "$prompt");
24 function interactive(name, doc, handler)
26     keywords(arguments);
27     var cmd = {
28         name: name,
29         handler: handler,
30         browser_object: arguments.$browser_object,
31         prefix: arguments.$prefix,
32         doc: doc,
33         shortdoc: get_shortdoc_string(doc),
34         prompt: arguments.$prompt,
35         source_code_reference: get_caller_source_code_reference() };
37     interactive_commands.put(name, cmd);
38     return name;
41 function interactive_error(str) {
42     var e = new Error(str);
43     e.__proto__ = interactive_error.prototype;
44     return e;
46 interactive_error.prototype.__proto__ = Error.prototype;
48 function interactive_context() {}
49 interactive_context.prototype = {
51     get P () this.prefix_argument,
53     get p () univ_arg_to_number(this.prefix_argument),
55     set p (default_value) univ_arg_to_number(this.prefix_argument, default_value),
57     get minibuffer () this.window.minibuffer,
59     get : function (x) this.buffer.get(x)
62 function handle_interactive_error(window, e) {
63     if (e instanceof interactive_error)
64         window.minibuffer.message(e.message);
65     else if (e instanceof abort)
66         window.minibuffer.message("Quit");
67     else {
68         dump_error(e);
69         window.minibuffer.message("call interactively: " + e);
70     }
73 function call_interactively(I, command)
75     var handler;
76     var top_args;
78     I.__proto__ = interactive_context.prototype;
80     if (I.buffer == null)
81         I.buffer = I.window.buffers.current;
82     else if (I.window == null)
83         I.window = I.buffer.window;
85     var window = I.window;
87     if (typeof(command) == "function") {
88         // Special interactive command
89         command(I);
90         return;
91     }
93     var cmd = interactive_commands.get(command);
94     if (!cmd)
95     {
96         window.minibuffer.message("Invalid command: " + command);
97         return;
98     }
100     I.command = cmd;
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)))
105     {
106         I.browser_object = cmd.browser_object;
107     }
109     // if no browser-object was specified interactively, use a
110     // default, possibly specified by the page-mode.
111     if (I.browser_object == null) {
112         I.browser_object =
113             I.buffer.default_browser_object_classes[command] ||
114             cmd.browser_object;
115     }
117     handler = cmd.handler;
119     try {
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));
125             }
126         }
128         var result = handler(I);
129         if (is_coroutine(result)) {
130             co_call(function() {
131                 try {
132                     yield result;
133                 } catch (e) {
134                     handle_interactive_error(window, e);
135                 }
136             }());
137         }
138     } catch (e)
139     {
140         handle_interactive_error(window, e);
141     }
145 function alternates () {
146     let alts = Array.prototype.slice.call(arguments, 0);
147     return function (I) {
148         var index = 0;
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) {
152                 num = num / 4;
153                 index++;
154             }
155         }
156         yield alts[index](I);
157     };
162  * Utility functions for use in the rc to alter the behavior
163  * of interactive commands.
164  */
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;