1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/**
* (C) Copyright 2007-2008 Jeremy Maitin-Shepard
*
* Use, modification, and distribution are subject to the terms specified in the
* COPYING file.
**/
/* This should only be used for minibuffer states where it makes
* sense. In particular, it should not be used if additional cleanup
* must be done. */
function minibuffer_abort (window)
{
var m = window.minibuffer;
var s = m.current_state;
if (s == null)
throw "Invalid minibuffer state";
m.pop_state();
}
interactive("minibuffer-abort", function (I) {minibuffer_abort(I.window);});
define_builtin_commands(
"minibuffer-",
function (I, command) {
try {
var m = I.minibuffer;
if (m._input_mode_enabled)
{
m._restore_normal_state();
var e = m.input_element;
var c = e.controllers.getControllerForCommand(command);
try {
m.ignore_input_events = true;
if (c && c.isCommandEnabled(command))
c.doCommand(command);
} finally {
m.ignore_input_events = false;
}
var s = m.current_state;
if (s.ran_minibuffer_command)
s.ran_minibuffer_command(command);
}
} catch (e)
{
/* Ignore exceptions. */
}
},
function (I) {
I.minibuffer.current_state.mark_active = !I.minibuffer.current_state.mark_active;
},
function (I) I.minibuffer.current_state.mark_active
);
function minibuffer_state(keymap, use_input_mode)
{
this.keymap = keymap;
this.use_input_mode = use_input_mode;
}
minibuffer_state.prototype.load = function () {}
minibuffer_state.prototype.unload = function () {}
minibuffer_state.prototype.destroy = function () {}
function minibuffer_message_state(keymap, message, destroy_function)
{
minibuffer_state.call(this, keymap, false);
this._message = message;
if (destroy_function)
this.destroy = destroy_function;
}
minibuffer_message_state.prototype = {
__proto__: minibuffer_state.prototype,
load : function (window) {
this.window = window;
},
unload : function (window) {
this.window = null;
},
get message () { return this._message; },
set message (x) {
if (this.window) {
this.window.minibuffer._restore_normal_state();
this.window.minibuffer._show(this._message);
}
}
};
function minibuffer_input_state(keymap, prompt, input, selection_start, selection_end)
{
this.prompt = prompt;
if (input)
this.input = input;
else
this.input = "";
if (selection_start)
this.selection_start = selection_start;
else
this.selection_start = 0;
if (selection_end)
this.selection_end = selection_end;
else
this.selection_end = this.selection_start;
minibuffer_state.call(this, keymap, true);
}
minibuffer_input_state.prototype.__proto__ = minibuffer_state.prototype;
/**
* The parameter `args' is an object specifying the arguments for
* basic_minibuffer_state. The following properties of args must/may
* be set:
*
* prompt: [required]
*
* initial_value: [optional] specifies the initial text
*
* select: [optional] specifies to select the initial text if set to non-null
*/
define_keywords("$prompt", "$initial_value", "$select");
function basic_minibuffer_state()
{
keywords(arguments);
var initial_value = arguments.$initial_value || "";
var sel_start, sel_end;
if (arguments.$select)
{
sel_start = 0;
sel_end = initial_value.length;
} else {
sel_start = sel_end = initial_value.length;
}
minibuffer_input_state.call(this, minibuffer_base_keymap,
arguments.$prompt, initial_value,
sel_start, sel_end);
}
basic_minibuffer_state.prototype.__proto__ = minibuffer_input_state.prototype; // inherit from minibuffer_state
define_variable("minibuffer_input_mode_show_message_timeout", 1000, "Time duration (in milliseconds) to flash minibuffer messages while in minibuffer input mode.");
function minibuffer (window)
{
this.element = window.document.getElementById("minibuffer");
this.output_element = window.document.getElementById("minibuffer-message");
this.input_prompt_element = window.document.getElementById("minibuffer-prompt");
this.input_element = window.document.getElementById("minibuffer-input");
var m = this;
this.input_element.inputField.addEventListener("blur", function() {
if (m.active && m._input_mode_enabled && !m._showing_message)
{
window.setTimeout(
function(){
m.input_element.inputField.focus();
}, 0);
}
}, false);
this.input_element.addEventListener("input", function(e) {
if (m.ignore_input_events || !m._input_mode_enabled)
return;
var s = m.current_state;
if (s) {
if (s.handle_input)
s.handle_input(m);
}
}, true);
// Ensure that the input area will have focus if a message is
// currently being flashed so that the default handler for key
// events will properly add text to the input area.
window.addEventListener("keydown", function (e) {
if (m._input_mode_enabled && m._showing_message)
m._restore_normal_state();
}, true);
this.window = window;
this.last_message = "";
this.states = [];
}
minibuffer.prototype = {
constructor : minibuffer.constructor,
get _selection_start () { return this.input_element.selectionStart; },
get _selection_end () { return this.input_element.selectionEnd; },
get _input_text () { return this.input_element.value; },
set _input_text (text) { this.input_element.value = text; },
get prompt () { return this.input_prompt_element.value; },
set prompt (s) { this.input_prompt_element.value = s; },
set_input_state : function(x) {
this._input_text = x[0];
this._set_selection(x[1], x[2]);
},
_set_selection : function (start, end) {
if (start == null)
start = this._input_text.length;
if (end == null)
end = this._input_text.length;
this.input_element.setSelectionRange(start,end);
},
/* Saved focus state */
saved_focused_frame : null,
saved_focused_element : null,
default_message : "",
current_message : null,
/* This method will display the specified string in the
* minibuffer, without recording it in any log/Messages buffer. */
show : function (str, force) {
if (!this.active || force) {
this.current_message = str;
this._show(str);
}
},
_show : function (str, force) {
if (this.last_message != str)
{
this.output_element.value = str;
this.last_message = str;
}
},
message : function (str) {
/* TODO: add the message to a *Messages* buffer, and/or
* possibly dump them to the console. */
this.show(str, true /* force */);
if (str.length > 0 && this.active)
this._flash_temporary_message();
},
clear : function () {
this.current_message = null;
if (!this.active)
this._show(this.default_message);
},
set_default_message : function (str) {
this.default_message = str;
if (this.current_message == null)
this._show(str);
},
get current_state () {
if (this.states.length == 0)
return null;
return this.states[this.states.length - 1];
},
push_state : function (state) {
this._save_state();
this.states.push(state);
this._restore_state();
},
pop_state : function () {
this.current_state.destroy();
this.states.pop();
this._restore_state();
},
pop_all : function () {
while (this.states.length > 0) {
this.current_state.destroy();
this.states.pop();
}
},
remove_state : function (state) {
var i = this.states.indexOf(state);
if (i == -1)
return;
var was_current = (i == (this.states.length - 1));
state.destroy();
this.states.splice(i, 1);
if (was_current)
this._restore_state();
},
_input_mode_enabled : false,
active : false,
/* If _input_mode_enabled is true, this is set to indicate that
* the message area is being temporarily shown instead of the
* input box. */
_showing_message : false,
_message_timer_ID : null,
/* This must only be called if _input_mode_enabled is true */
_restore_normal_state : function () {
if (this._showing_message)
{
this.window.clearTimeout(this._message_timer_ID);
this._message_timer_ID = null;
this._showing_message = false;
if (this._input_mode_enabled)
this._switch_to_input_mode();
else
this._show(this.current_state._message);
}
},
/* This must only be called if _input_mode_enabled is true */
_flash_temporary_message : function () {
if (this._showing_message)
this.window.clearTimeout(this._message_timer_ID);
else {
this._showing_message = true;
if (this._input_mode_enabled)
this._switch_to_message_mode();
}
var obj = this;
this._message_timer_ID = this.window.setTimeout(function(){
obj._restore_normal_state();
}, minibuffer_input_mode_show_message_timeout);
},
_switch_to_input_mode : function () {
this.element.setAttribute("minibuffermode", "input");
this.input_element.inputField.focus();
},
_switch_to_message_mode : function () {
this.element.setAttribute("minibuffermode", "message");
},
_restore_state : function () {
var s = this.current_state;
var want_input_mode = false;
if (s) {
if (!this.active) {
this.saved_focused_frame = this.window.document.commandDispatcher.focusedWindow;
this.saved_focused_element = this.window.document.commandDispatcher.focusedElement;
}
if (s.use_input_mode) {
want_input_mode = true;
this._input_text = s.input;
this.prompt = s.prompt;
this._set_selection(s.selection_start, s.selection_end);
} else {
this._show(s._message);
}
s.load(this.window);
this.window.keyboard.set_override_keymap(s.keymap);
this.active = true;
} else {
if (this.active) {
this.active = false;
this.window.keyboard.set_override_keymap(null);
if (this.saved_focused_element)
set_focus_no_scroll(this.window, this.saved_focused_element);
else if (this.saved_focused_frame)
set_focus_no_scroll(this.window, this.saved_focused_frame);
this.saved_focused_element = null;
this.saved_focused_frame = null;
this._show(this.current_message || this.default_message);
}
}
var in_input_mode = this._input_mode_enabled && !this._showing_message;
if (this._showing_message) {
this.window.clearTimeout(this._message_timer_ID);
this._message_timer_ID = null;
this._showing_message = false;
}
if (want_input_mode && !in_input_mode)
this._switch_to_input_mode();
else if (!want_input_mode && in_input_mode)
this._switch_to_message_mode();
this._input_mode_enabled = want_input_mode;
},
_save_state : function () {
var s = this.current_state;
if (s)
{
if (s.use_input_mode) {
s.input = this._input_text;
s.prompt = this.prompt;
s.selection_start = this._selection_start;
s.selection_end = this._selection_end;
}
s.unload(this.window);
}
},
insert_before : function (element) {
this.element.parentNode.insertBefore(element, this.element);
}
};
function minibuffer_initialize_window(window)
{
window.minibuffer = new minibuffer(window);
}
add_hook("window_initialize_early_hook", minibuffer_initialize_window);
function minibuffer_window_close_handler(window) {
window.minibuffer.pop_all();
}
add_hook("window_close_hook", minibuffer_window_close_handler);
/* Note: This is concise, but doesn't seem to be useful in practice,
* because nothing can be done with the state alone. */
minibuffer.prototype.check_state = function(type) {
var s = this.current_state;
if (!(s instanceof type))
throw new Error("Invalid minibuffer state.");
return s;
};
minibuffer.prototype.show_wait_message = function (initial_message, destroy_function) {
var s = new minibuffer_message_state(minibuffer_message_keymap, initial_message, destroy_function);
this.push_state(s);
return s;
};
minibuffer.prototype.wait_for = function minibuffer__wait_for(message, coroutine) {
var cc = yield CONTINUATION;
var done = false;
var s = this.show_wait_message(message, function () { if (!done) cc.throw(abort()); });
var result;
try {
result = yield coroutine;
}
finally {
done = true;
this.remove_state(s);
}
yield co_return(result);
}
|