2 * (C) Copyright 2007-2010 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 = {};
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 (minibuffer, continuation) {
51 keywords(arguments, $keymap = minibuffer_keymap);
53 basic_minibuffer_state.call(this, minibuffer, forward_keywords(arguments));
55 this.continuation = continuation;
56 if (arguments.$history) {
57 this.history = minibuffer_history_data[arguments.$history] =
58 minibuffer_history_data[arguments.$history] || [];
59 this.history_index = -1;
60 this.saved_last_history_entry = null;
63 this.validator = arguments.$validator;
65 if (arguments.$completer != null) {
66 this.completer = arguments.$completer;
67 let auto = arguments.$auto_complete;
68 while (typeof(auto) == "string")
69 auto = minibuffer_auto_complete_preferences[auto];
71 auto = minibuffer_auto_complete_default;
72 this.auto_complete = auto;
73 this.auto_complete_initial = !!arguments.$auto_complete_initial;
74 this.auto_complete_conservative = !!arguments.$auto_complete_conservative;
75 let delay = arguments.$auto_complete_delay;
77 delay = default_minibuffer_auto_complete_delay;
78 this.auto_complete_delay = delay;
79 this.completions = null;
80 this.completions_valid = false;
81 this.space_completes = !!arguments.$space_completes;
82 if (this.space_completes)
83 this.keymaps.push(minibuffer_space_completion_keymap);
84 this.completions_timer_ID = null;
85 this.completions_display_element = null;
86 this.selected_completion_index = -1;
87 this.match_required = !!arguments.$match_required;
88 this.match_required_default = this.match_required;
89 if (this.match_required)
90 this.default_completion = arguments.$default_completion;
94 function completions_tree_view (minibuffer_state) {
95 this.minibuffer_state = minibuffer_state;
98 var atom_service = Cc["@mozilla.org/atom-service;1"].getService(Ci.nsIAtomService);
100 completions_tree_view.prototype = {
101 constructor: completions_tree_view,
103 var c = this.minibuffer_state.completions;
108 getCellText: function (row,column) {
109 var c = this.minibuffer_state.completions;
112 if (column.index == 0)
113 return c.get_string(row);
114 if (c.get_description)
115 return c.get_description(row);
118 setTree: function (treebox) { this.treebox = treebox; },
119 isContainer: function (row) { return false; },
120 isSeparator: function (row) { return false; },
121 isSorted: function () { return false; },
122 getLevel: function (row) { return 0; },
123 getImageSrc: function (row, col) { return null; },
124 getRowProperties: function (row, props) {},
125 getCellProperties: function (row, col, props) {
127 props.AppendElement(atom_service.getAtom("completion-string"));
129 props.AppendElement(atom_service.getAtom("completion-description"));
131 getColumnProperties: function (colid, col, props) {}
134 // inherit from basic_minibuffer_state
135 text_entry_minibuffer_state.prototype = {
136 constructor: text_entry_minibuffer_state,
137 __proto__: basic_minibuffer_state.prototype,
139 basic_minibuffer_state.prototype.load.call(this);
140 var window = this.minibuffer.window;
141 if (this.completer) {
142 // Create completion display element if needed
143 if (!this.completion_element) {
144 /* FIXME: maybe use the dom_generator */
145 var tree = create_XUL(window, "tree");
147 tree.addEventListener("select", function () {
148 s.selected_completion_index = s.completions_display_element.currentIndex;
149 s.handle_completion_selected();
151 tree.setAttribute("class", "completions");
153 tree.setAttribute("rows", "8");
155 tree.setAttribute("collapsed", "true");
157 tree.setAttribute("hidecolumnpicker", "true");
158 tree.setAttribute("hideheader", "true");
160 var treecols = create_XUL(window, "treecols");
161 tree.appendChild(treecols);
162 var treecol = create_XUL(window, "treecol");
163 treecol.setAttribute("flex", "1");
164 treecols.appendChild(treecol);
165 treecol = create_XUL(window, "treecol");
166 treecol.setAttribute("flex", "1");
167 treecols.appendChild(treecol);
168 tree.appendChild(create_XUL(window, "treechildren"));
170 this.minibuffer.insert_before(tree);
171 tree.view = new completions_tree_view(this);
172 this.completions_display_element = tree;
174 /* This is the initial loading of this minibuffer
175 * state. If this.complete_initial is true, generate
177 if (this.auto_complete_initial)
180 this.update_completions_display();
184 unload: function () {
185 if (this.completions_display_element)
186 this.completions_display_element.setAttribute("collapsed", "true");
187 basic_minibuffer_state.prototype.unload.call(this);
190 destroy: function () {
191 if (this.completions != null && this.completions.destroy)
192 this.completions.destroy();
193 delete this.completions;
194 if (this.completions_cont)
195 this.completions_cont.throw(abort());
196 delete this.completions_cont;
198 var el = this.completions_display_element;
200 el.parentNode.removeChild(el);
201 this.completions_display_element = null;
203 if (this.continuation)
204 this.continuation.throw(abort());
205 basic_minibuffer_state.prototype.destroy.call(this);
208 handle_input: function () {
209 if (!this.completer) return;
211 this.completions_valid = false;
213 if (!this.auto_complete) return;
216 var window = this.minibuffer.window;
218 if (this.auto_complete_delay > 0) {
219 if (this.completions_timer_ID != null)
220 window.clearTimeout(this.completions_timer_ID);
221 this.completions_timer_ID = window.setTimeout(
223 s.completions_timer_ID = null;
224 s.update_completions(true /* auto */, true /* update completions display */);
225 }, this.auto_complete_delay);
228 s.update_completions(true /* auto */, true /* update completions display */);
231 update_completions_display: function () {
232 var m = this.minibuffer;
233 if (m.current_state == this) {
234 if (this.completions && this.completions.count > 0) {
235 this.completions_display_element.view = this.completions_display_element.view;
236 this.completions_display_element.setAttribute("collapsed", "false");
238 this.completions_display_element.currentIndex = this.selected_completion_index;
239 this.completions_display_element.treeBoxObject.scrollToRow(this.selected_completion_index);
241 this.completions_display_element.setAttribute("collapsed", "true");
246 /* If auto is true, this update is due to auto completion, rather
247 * than specifically requested. */
248 update_completions: function (auto, update_display) {
249 var window = this.minibuffer.window;
250 if (this.completions_timer_ID != null) {
251 window.clearTimeout(this.completions_timer_ID);
252 this.completions_timer_ID = null;
255 let m = this.minibuffer;
257 if (this.completions_cont) {
258 this.completions_cont.throw(abort());
259 this.completions_cont = null;
262 let c = this.completer(m._input_text, m._selection_start,
263 auto && this.auto_complete_conservative);
265 if (is_coroutine(c)) {
267 let already_done = false;
268 this.completions_cont = co_call(function () {
273 handle_interactive_error(window, e);
275 s.completions_cont = null;
278 s.update_completions_done(x, update_display);
281 // In case the completer actually already finished
283 this.completions_cont = null;
286 this.update_completions_done(c, update_display);
289 update_completions_done: function (c, update_display) {
290 /* The completer should return undefined if completion was not
291 * attempted due to auto being true. Otherwise, it can return
292 * null to indicate no completions. */
293 if (this.completions != null && this.completions.destroy)
294 this.completions.destroy();
296 this.completions = c;
297 this.completions_valid = true;
298 this.applied_common_prefix = false;
300 if (c && ("get_match_required" in c))
301 this.match_required = c.get_match_required();
302 if (this.match_required == null)
303 this.match_required = this.match_required_default;
306 if (c && c.count > 0) {
307 if (this.match_required) {
310 else if (c.default_completion != null)
311 i = c.default_completion;
312 else if (this.default_completion && this.completions.index_of)
313 i = this.completions.index_of(this.default_completion);
315 this.selected_completion_index = i;
319 this.update_completions_display();
322 select_completion: function (i) {
323 this.selected_completion_index = i;
324 this.completions_display_element.currentIndex = i;
326 this.completions_display_element.treeBoxObject.ensureRowIsVisible(i);
327 this.handle_completion_selected();
330 handle_completion_selected: function () {
332 * When a completion is selected, apply it to the input text
333 * if a match is not "required"; otherwise, the completion is
336 var i = this.selected_completion_index;
337 var m = this.minibuffer;
338 var c = this.completions;
340 if (this.completions_valid && c && !this.match_required && i >= 0 && i < c.count)
341 m.set_input_state(c.get_input_state(i));
345 function minibuffer_complete (window, count) {
346 var m = window.minibuffer;
347 var s = m.current_state;
348 if (!(s instanceof text_entry_minibuffer_state))
349 throw new Error("Invalid minibuffer state");
352 var just_completed_manually = false;
353 if (!s.completions_valid || s.completions === undefined) {
354 if (s.completions_timer_ID == null)
355 just_completed_manually = true;
356 //XXX: may need to use ignore_input_events here
357 s.update_completions(false /* not auto */, true /* update completions display */);
359 // If the completer is a coroutine, nothing we can do here
360 if (!s.completions_valid)
364 var c = s.completions;
366 if (!c || c.count == 0)
369 var e = s.completions_display_element;
374 if (count == 1 && !s.applied_common_prefix && (common_prefix = c.common_prefix_input_state)) {
375 //XXX: may need to use ignore_input_events here
376 m.set_input_state(common_prefix);
377 s.applied_common_prefix = true;
378 } else if (!just_completed_manually) {
379 if (e.currentIndex != -1) {
380 new_index = (e.currentIndex + count) % c.count;
382 new_index += c.count;
384 new_index = (count - 1) % c.count;
386 new_index += c.count;
390 if (new_index != -1) {
392 m.ignore_input_events = true;
393 s.select_completion(new_index);
395 m.ignore_input_events = false;
399 interactive("minibuffer-complete", null,
400 function (I) { minibuffer_complete(I.window, I.p); });
401 interactive("minibuffer-complete-previous", null,
402 function (I) { minibuffer_complete(I.window, -I.p); });
404 function exit_minibuffer (window) {
405 var m = window.minibuffer;
406 var s = m.current_state;
407 if (!(s instanceof text_entry_minibuffer_state))
408 throw new Error("Invalid minibuffer state");
410 var val = m._input_text;
412 if (s.validator != null && !s.validator(val, m))
417 if (s.completer && s.match_required) {
418 if (!s.completions_valid || s.completions === undefined)
419 s.update_completions(false /* not conservative */, false /* don't update */);
421 let c = s.completions;
422 let i = s.selected_completion_index;
423 if (c != null && i >= 0 && i < c.count) {
424 if (c.get_value != null)
425 match = c.get_value(i);
427 match = c.get_string(i);
429 m.message("No match");
436 if (s.history.length > minibuffer_history_max_items)
437 s.history.splice(0, s.history.length - minibuffer_history_max_items);
439 var cont = s.continuation;
440 delete s.continuation;
443 if (s.match_required)
449 interactive("exit-minibuffer", null,
450 function (I) { exit_minibuffer(I.window); });
452 function minibuffer_history_next (window, count) {
453 var m = window.minibuffer;
454 var s = m.current_state;
455 if (!(s instanceof text_entry_minibuffer_state))
456 throw new Error("Invalid minibuffer state");
457 if (!s.history || s.history.length == 0)
458 throw interactive_error("No history available.");
461 var index = s.history_index;
462 if (count > 0 && index == -1)
463 throw interactive_error("End of history; no next item");
464 else if (count < 0 && index == 0)
465 throw interactive_error("Beginning of history; no preceding item");
467 s.saved_last_history_entry = m._input_text;
468 index = s.history.length + count;
470 index = index + count;
475 m._restore_normal_state();
476 if (index >= s.history.length) {
478 m._input_text = s.saved_last_history_entry;
480 m._input_text = s.history[index];
482 s.history_index = index;
486 interactive("minibuffer-history-next", null,
487 function (I) { minibuffer_history_next(I.window, I.p); });
488 interactive("minibuffer-history-previous", null,
489 function (I) { minibuffer_history_next(I.window, -I.p); });
491 // Define the asynchronous minibuffer.read function
492 minibuffer.prototype.read = function () {
493 var s = new text_entry_minibuffer_state(this, (yield CONTINUATION), forward_keywords(arguments));
495 var result = yield SUSPEND;
496 yield co_return(result);
499 minibuffer.prototype.read_command = function () {
502 $prompt = "Command", $history = "command",
503 $completer = prefix_completer(
504 $completions = function (visitor) interactive_commands.for_each_value(visitor),
505 $get_string = function (x) x.name,
506 $get_description = function (x) x.shortdoc || "",
507 $get_value = function (x) x.name),
510 var result = yield this.read(forward_keywords(arguments));
511 yield co_return(result);
514 minibuffer.prototype.read_user_variable = function () {
517 $prompt = "User variable", $history = "user_variable",
518 $completer = prefix_completer(
519 $completions = function (visitor) {
520 for (var i in user_variables) visitor(i);
522 $get_string = function (x) x,
523 $get_description = function (x) user_variables[x].shortdoc || "",
524 $get_value = function (x) x),
527 var result = yield this.read(forward_keywords(arguments));
528 yield co_return(result);
531 minibuffer.prototype.read_preference = function () {
533 $prompt = "Preference:", $history = "preference",
534 $completer = prefix_completer(
535 $completions = preferences.getBranch(null).getChildList("", {}),
536 $get_description = function (pref) {
537 let default_value = get_default_pref(pref);
538 let value = get_pref(pref);
539 if (value == default_value)
542 switch (preferences.getBranch(null).getPrefType(pref)) {
543 case Ci.nsIPrefBranch.PREF_STRING:
546 case Ci.nsIPrefBranch.PREF_INT:
549 case Ci.nsIPrefBranch.PREF_BOOL:
553 let out = type + ":";
555 out += " " + pretty_print_value(value);
556 if (default_value != null)
557 out += " (" + pretty_print_value(default_value) + ")";
562 var result = yield this.read(forward_keywords(arguments));
563 yield co_return(result);
566 provide("minibuffer-read");