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 * options: An object that may contain any of the following keys.
21 * Options may be null.
23 * prefix: boolean where `true' means that this command is a
26 function interactive(name, doc, handler, options)
28 options = merge_defaults(options);
32 prefix: options.prefix,
34 shortdoc: get_shortdoc_string(doc),
35 source_code_reference: get_caller_source_code_reference() };
37 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;
47 function interactive_context() {}
48 interactive_context.prototype = {
50 get P () this.prefix_argument,
52 get p () univ_arg_to_number(this.prefix_argument),
54 set p (default_value) univ_arg_to_number(this.prefix_argument, default_value),
56 get minibuffer () this.window.minibuffer,
58 get : function (x) this.buffer.get(x)
61 function handle_interactive_error(window, e) {
62 if (e instanceof interactive_error)
63 window.minibuffer.message(e.message);
64 else if (e instanceof abort)
65 window.minibuffer.message("Quit");
68 window.minibuffer.message("call interactively: " + e);
72 // Any additional arguments specify "given" arguments to the function.
73 function call_interactively(I, command)
78 I.__proto__ = interactive_context.prototype;
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
93 var cmd = interactive_commands.get(command);
96 window.minibuffer.message("Invalid command: " + command);
102 var handler = cmd.handler;
105 var result = handler(I);
106 if (is_coroutine(result)) {
111 handle_interactive_error(window, e);
117 handle_interactive_error(window, e);
122 I.f = interactive_method(
123 $doc = "Existing file",
124 $async = function (ctx, cont) {
125 keywords(arguments, $prompt = "File:", $initial_value = default_directory.path,
127 ctx.window.minibuffer.read(
128 $prompt = arguments.$prompt,
129 $initial_value = arguments.$initial_value,
130 $history = arguments.$history,
131 $callback = function(s) {
132 var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
138 // FIXME: eventually they will differ, when completion for files is added