2 * (C) Copyright 2007-2009 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 define_variable("default_minibuffer_auto_complete_delay", 150,
12 "Delay (in milliseconds) after the most recent key-stroke "+
13 "before auto-completing.");
15 define_variable("minibuffer_auto_complete_preferences", {});
17 define_variable("minibuffer_auto_complete_default", false,
18 "Boolean specifying whether to auto-complete by default. "+
19 "The user variable `minibuffer_auto_complete_preferences' "+
22 var minibuffer_history_data = new string_hashmap();
24 /* FIXME: These should possibly be saved to disk somewhere */
25 define_variable("minibuffer_history_max_items", 100,
26 "Maximum number of minibuffer history entries stored. Older "+
27 "history entries are truncated after this limit is reached.");
30 /* The parameter `args' specifies the arguments. In addition, the
31 * arguments for basic_minibuffer_state are also allowed.
33 * history: [optional] specifies a string to identify the history list to use
39 * default_completion only used if match_required is set to true
41 * $valiator [optional]
42 * specifies a function
44 define_keywords("$keymap", "$history", "$validator",
45 "$completer", "$match_required", "$default_completion",
46 "$auto_complete", "$auto_complete_initial", "$auto_complete_conservative",
47 "$auto_complete_delay",
49 /* FIXME: support completing in another thread */
50 function text_entry_minibuffer_state (window, continuation) {
51 keywords(arguments, $keymap = minibuffer_keymap);
53 basic_minibuffer_state.call(this, window, forward_keywords(arguments));
55 this.continuation = continuation;
56 if (arguments.$history) {
57 this.history = minibuffer_history_data.get_put_default(arguments.$history, []);
58 this.history_index = -1;
59 this.saved_last_history_entry = null;
62 this.validator = arguments.$validator;
64 if (arguments.$completer != null) {
65 this.completer = arguments.$completer;
66 let auto = arguments.$auto_complete;
67 while (typeof(auto) == "string")
68 auto = minibuffer_auto_complete_preferences[auto];
70 auto = minibuffer_auto_complete_default;
71 this.auto_complete = auto;
72 this.auto_complete_initial = !!arguments.$auto_complete_initial;
73 this.auto_complete_conservative = !!arguments.$auto_complete_conservative;
74 let delay = arguments.$auto_complete_delay;
76 delay = default_minibuffer_auto_complete_delay;
77 this.auto_complete_delay = delay;
78 this.completions = null;
79 this.completions_valid = false;
80 this.space_completes = !!arguments.$space_completes;
81 this.completions_timer_ID = null;
82 this.completions_display_element = null;
83 this.selected_completion_index = -1;
84 this.match_required = !!arguments.$match_required;
85 this.match_required_default = this.match_required;
86 if (this.match_required)
87 this.default_completion = arguments.$default_completion;
91 function completions_tree_view (minibuffer_state) {
92 this.minibuffer_state = minibuffer_state;
95 var atom_service = Cc["@mozilla.org/atom-service;1"].getService(Ci.nsIAtomService);
97 completions_tree_view.prototype = {
99 var c = this.minibuffer_state.completions;
104 getCellText : function (row,column) {
105 var c = this.minibuffer_state.completions;
108 if (column.index == 0)
109 return c.get_string(row);
110 if (c.get_description)
111 return c.get_description(row);
114 setTree : function (treebox) { this.treebox = treebox; },
115 isContainer: function (row) { return false; },
116 isSeparator: function (row) { return false; },
117 isSorted: function () { return false; },
118 getLevel: function (row) { return 0; },
119 getImageSrc: function (row, col) { return null; },
120 getRowProperties: function (row, props) {},
121 getCellProperties: function (row, col, props) {
123 props.AppendElement(atom_service.getAtom("completion-string"));
125 props.AppendElement(atom_service.getAtom("completion-description"));
127 getColumnProperties: function (colid, col, props) {}
130 // inherit from basic_minibuffer_state
131 text_entry_minibuffer_state.prototype = {
132 __proto__: basic_minibuffer_state.prototype,
133 load : function (window) {
134 basic_minibuffer_state.prototype.load.call(this, window);
135 this.window = window;
136 if (this.completer) {
137 // Create completion display element if needed
138 if (!this.completion_element) {
139 /* FIXME: maybe use the dom_generator */
140 var tree = create_XUL(window, "tree");
142 tree.addEventListener("select", function () {
143 s.selected_completion_index = s.completions_display_element.currentIndex;
144 s.handle_completion_selected();
146 tree.setAttribute("class", "completions");
148 tree.setAttribute("rows", "8");
150 tree.setAttribute("collapsed", "true");
152 tree.setAttribute("hidecolumnpicker", "true");
153 tree.setAttribute("hideheader", "true");
155 var treecols = create_XUL(window, "treecols");
156 tree.appendChild(treecols);
157 var treecol = create_XUL(window, "treecol");
158 treecol.setAttribute("flex", "1");
159 treecols.appendChild(treecol);
160 treecol = create_XUL(window, "treecol");
161 treecol.setAttribute("flex", "1");
162 treecols.appendChild(treecol);
163 tree.appendChild(create_XUL(window, "treechildren"));
165 window.minibuffer.insert_before(tree);
166 tree.view = new completions_tree_view(this);
167 this.completions_display_element = tree;
169 /* This is the initial loading of this minibuffer
170 * state. If this.complete_initial is true, generate
172 if (this.auto_complete_initial)
176 this.update_completions_display();
180 unload : function (window) {
181 if (this.completions_display_element)
182 this.completions_display_element.setAttribute("collapsed", "true");
183 basic_minibuffer_state.prototype.unload.call(this, window);
186 destroy : function (window) {
187 if (this.completions != null && this.completions.destroy)
188 this.completions.destroy();
189 delete this.completions;
190 if (this.completions_cont)
191 this.completions_cont.throw(abort());
192 delete this.completions_cont;
194 var el = this.completions_display_element;
196 el.parentNode.removeChild(el);
197 this.completions_display_element = null;
199 if (this.continuation)
200 this.continuation.throw(abort());
201 basic_minibuffer_state.prototype.destroy.call(this, window);
204 handle_input : function () {
205 if (!this.completer) return;
207 this.completions_valid = false;
209 if (!this.auto_complete) return;
213 if (this.auto_complete_delay > 0) {
214 if (this.completions_timer_ID != null)
215 this.window.clearTimeout(this.completions_timer_ID);
216 this.completions_timer_ID = this.window.setTimeout(
218 s.completions_timer_ID = null;
219 s.update_completions(true /* auto */, true /* update completions display */);
220 }, this.auto_complete_delay);
224 s.update_completions(true /* auto */, true /* update completions display */);
227 ran_minibuffer_command : function () {
231 update_completions_display : function () {
233 var m = this.window.minibuffer;
235 if (m.current_state == this) {
236 if (this.completions && this.completions.count > 0) {
237 this.completions_display_element.view = this.completions_display_element.view;
238 this.completions_display_element.setAttribute("collapsed", "false");
240 this.completions_display_element.currentIndex = this.selected_completion_index;
241 this.completions_display_element.treeBoxObject.scrollToRow(this.selected_completion_index);
243 this.completions_display_element.setAttribute("collapsed", "true");
248 /* If auto is true, this update is due to auto completion, rather
249 * than specifically requested. */
250 update_completions : function (auto, update_display) {
252 if (this.completions_timer_ID != null) {
253 this.window.clearTimeout(this.completions_timer_ID);
254 this.completions_timer_ID = null;
257 let m = this.window.minibuffer;
259 if (this.completions_cont) {
260 this.completions_cont.throw(abort());
261 this.completions_cont = null;
264 let c = this.completer(m._input_text, m._selection_start,
265 auto && this.auto_complete_conservative);
267 if (is_coroutine(c)) {
269 let already_done = false;
270 this.completions_cont = co_call(function () {
275 handle_interactive_error(m.window, e);
277 s.completions_cont = null;
280 s.update_completions_done(x, update_display);
283 // In case the completer actually already finished
285 this.completions_cont = null;
288 this.update_completions_done(c, update_display);
291 update_completions_done : function update_completions_done (c, update_display) {
293 /* The completer should return undefined if completion was not
294 * attempted due to auto being true. Otherwise, it can return
295 * null to indicate no completions. */
296 if (this.completions != null && this.completions.destroy)
297 this.completions.destroy();
299 this.completions = c;
300 this.completions_valid = true;
301 this.applied_common_prefix = false;
303 if (c && ("get_match_required" in c))
304 this.match_required = c.get_match_required();
305 if (this.match_required == null)
306 this.match_required = this.match_required_default;
309 if (c && c.count > 0) {
310 if (this.match_required) {
313 else if (c.default_completion != null)
314 i = c.default_completion;
315 else if (this.default_completion && this.completions.index_of)
316 i = this.completions.index_of(this.default_completion);
318 this.selected_completion_index = i;
322 this.update_completions_display();
325 select_completion : function (i) {
326 this.selected_completion_index = i;
327 this.completions_display_element.currentIndex = i;
329 this.completions_display_element.treeBoxObject.ensureRowIsVisible(i);
330 this.handle_completion_selected();
333 handle_completion_selected : function () {
335 * When a completion is selected, apply it to the input text
336 * if a match is not "required"; otherwise, the completion is
339 var i = this.selected_completion_index;
340 var m = this.window.minibuffer;
341 var c = this.completions;
343 if (this.completions_valid && c && !this.match_required && i >= 0 && i < c.count)
345 m.set_input_state(c.get_input_state(i));
350 function minibuffer_complete (window, count) {
351 var m = window.minibuffer;
352 var s = m.current_state;
353 if (!(s instanceof text_entry_minibuffer_state))
354 throw new Error("Invalid minibuffer state");
357 var just_completed_manually = false;
358 if (!s.completions_valid || s.completions === undefined) {
359 if (s.completions_timer_ID == null)
360 just_completed_manually = true;
361 s.update_completions(false /* not auto */, true /* update completions display */);
363 // If the completer is a coroutine, nothing we can do here
364 if (!s.completions_valid)
368 var c = s.completions;
370 if (!c || c.count == 0)
373 var e = s.completions_display_element;
378 if (count == 1 && !s.applied_common_prefix && (common_prefix = c.common_prefix_input_state)) {
379 m.set_input_state(common_prefix);
380 s.applied_common_prefix = true;
381 } else if (!just_completed_manually) {
382 if (e.currentIndex != -1) {
383 new_index = (e.currentIndex + count) % c.count;
385 new_index += c.count;
387 new_index = (count - 1) % c.count;
389 new_index += c.count;
394 s.select_completion(new_index);
396 interactive("minibuffer-complete", null,
397 function (I) { minibuffer_complete(I.window, I.p); });
398 interactive("minibuffer-complete-previous", null,
399 function (I) { minibuffer_complete(I.window, -I.p); });
401 function exit_minibuffer (window) {
402 var m = window.minibuffer;
403 var s = m.current_state;
404 if (!(s instanceof text_entry_minibuffer_state))
405 throw new Error("Invalid minibuffer state");
407 var val = m._input_text;
409 if (s.validator != null && !s.validator(val, m))
414 if (s.completer && s.match_required) {
415 if (!s.completions_valid || s.completions === undefined)
416 s.update_completions(false /* not conservative */, false /* don't update */);
418 let c = s.completions;
419 let i = s.selected_completion_index;
420 if (c != null && i >= 0 && i < c.count) {
421 if (c.get_value != null)
422 match = c.get_value(i);
424 match = c.get_string(i);
426 m.message("No match");
433 if (s.history.length > minibuffer_history_max_items)
434 s.history.splice(0, s.history.length - minibuffer_history_max_items);
436 var cont = s.continuation;
437 delete s.continuation;
440 if (s.match_required)
446 interactive("exit-minibuffer", null,
447 function (I) { exit_minibuffer(I.window); });
449 function minibuffer_history_next (window, count) {
450 var m = window.minibuffer;
451 var s = m.current_state;
452 if (!(s instanceof text_entry_minibuffer_state))
453 throw new Error("Invalid minibuffer state");
454 if (!s.history || s.history.length == 0)
455 throw interactive_error("No history available.");
458 var index = s.history_index;
459 if (count > 0 && index == -1)
460 throw interactive_error("End of history; no next item");
461 else if (count < 0 && index == 0) {
462 throw interactive_error("Beginning of history; no preceding item");
465 s.saved_last_history_entry = m._input_text;
466 index = s.history.length + count;
468 index = index + count;
473 m._restore_normal_state();
474 if (index >= s.history.length) {
476 m._input_text = s.saved_last_history_entry;
478 m._input_text = s.history[index];
480 s.history_index = index;
484 interactive("minibuffer-history-next", null,
485 function (I) { minibuffer_history_next(I.window, I.p); });
486 interactive("minibuffer-history-previous", null,
487 function (I) { minibuffer_history_next(I.window, -I.p); });
489 // Define the asynchronous minibuffer.read function
490 minibuffer.prototype.read = function () {
491 var s = new text_entry_minibuffer_state(this.window, (yield CONTINUATION), forward_keywords(arguments));
493 var result = yield SUSPEND;
494 yield co_return(result);
497 minibuffer.prototype.read_command = function () {
500 $prompt = "Command", $history = "command",
501 $completer = prefix_completer(
502 $completions = function (visitor) interactive_commands.for_each_value(visitor),
503 $get_string = function (x) x.name,
504 $get_description = function (x) x.shortdoc || "",
505 $get_value = function (x) x.name),
506 $match_required = true);
507 var result = yield this.read(forward_keywords(arguments));
508 yield co_return(result);
511 minibuffer.prototype.read_user_variable = function () {
514 $prompt = "User variable", $history = "user_variable",
515 $completer = prefix_completer(
516 $completions = function (visitor) {
517 for (var i in user_variables) visitor(i);
519 $get_string = function (x) x,
520 $get_description = function (x) user_variables[x].shortdoc || "",
521 $get_value = function (x) x),
522 $match_required = true);
523 var result = yield this.read(forward_keywords(arguments));
524 yield co_return(result);
527 minibuffer.prototype.read_preference = function minibuffer__read_preference () {
529 $prompt = "Preference:", $history = "preference",
530 $completer = prefix_completer(
531 $completions = preferences.getBranch(null).getChildList("", {}),
532 $get_description = function (pref) {
533 let default_value = get_default_pref(pref);
534 let value = get_pref(pref);
535 if (value == default_value)
538 switch (preferences.getBranch(null).getPrefType(pref)) {
539 case Ci.nsIPrefBranch.PREF_STRING:
542 case Ci.nsIPrefBranch.PREF_INT:
545 case Ci.nsIPrefBranch.PREF_BOOL:
549 let out = type + ":";
551 out += " " + pretty_print_value(value);
552 if (default_value != null)
553 out += " (" + pretty_print_value(default_value) + ")";
556 $match_required = true);
557 var result = yield this.read(forward_keywords(arguments));
558 yield co_return(result);
561 provide("minibuffer-read");