browser_object_up_url: support trimming #ref (fragment)
[conkeror.git] / modules / interactive.js
blobee891ac9a1cb75dfdb68ea7dd3fc330476e1adb0
1 /**
2  * (C) Copyright 2007-2009 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 in_module(null);
11 require("utils.js");
13 var interactive_commands = new string_hashmap();
15 /**
16  * name: string name of the command.
17  *
18  * doc: Documentation string, may be null.
19  *
20  * handler: A function to handle the command.
21  *
22  * The $prefix keyword, when true, means that the command
23  * is a prefix-command.
24  */
25 define_keywords("$prefix", "$browser_object", "$prompt");
26 function interactive (name, doc, handler) {
27     keywords(arguments);
28     var cmd = {
29         name: name,
30         handler: handler,
31         browser_object: arguments.$browser_object,
32         prefix: arguments.$prefix,
33         doc: doc,
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);
39     return name;
42 function interactive_error (str) {
43     var e = new Error(str);
44     e.__proto__ = interactive_error.prototype;
45     return e;
47 interactive_error.prototype.__proto__ = Error.prototype;
50 function interactive_context (buffer) {
51     this.local = conkeror;
52     this.buffer = buffer;
53     if (buffer) {
54         this.window = this.buffer.window;
55         if (buffer.page) {
56             this.local = buffer.page.local;
57         } else {
58             this.local = buffer.local;
59         }
60     }
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) {
76     if (! window)
77         throw e;
78     if (e instanceof interactive_error) {
79         window.minibuffer.message(e.message);
80     } else if (e instanceof abort) {
81         window.minibuffer.message("Quit");
82     } else {
83         dump_error(e);
84         window.minibuffer.message("call interactively: " + e);
85     }
88 function call_interactively (I, command) {
89     var handler;
90     var window = I.window;
92     if (typeof command == "function") {
93         // Special interactive command
94         command(I);
95         yield co_return();
96     }
98     var cmd = interactive_commands.get(command);
99     if (!cmd) {
100         handle_interactive_error(
101             window,
102             interactive_error("Invalid command: " + command));
103         yield co_return();
104     }
106     I.command = cmd;
107     handler = cmd.handler;
109     try {
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));
115             }
116         }
118         try {
119             yield handler(I);
120         } catch (e) {
121             handle_interactive_error(window, e);
122         }
123     } catch (e) {
124         handle_interactive_error(window, e);
125     }
129 function alternates () {
130     let alts = Array.prototype.slice.call(arguments, 0);
131     return function (I) {
132         var index = 0;
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) {
136                 num = num / 4;
137                 index++;
138             }
139         }
140         yield alts[index](I);
141     };
146  * Utility functions for use in the rc to alter the behavior
147  * of interactive commands.
148  */
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");