File: minibuffer-read.js

package info (click to toggle)
conkeror 1.0.3%2Bgit170123-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,988 kB
  • sloc: ansic: 280; sh: 255; xml: 173; makefile: 69
file content (597 lines) | stat: -rw-r--r-- 21,740 bytes parent folder | download | duplicates (3)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/**
 * (C) Copyright 2007-2010,2012 John J. Foerch
 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

define_variable("default_minibuffer_auto_complete_delay", 150,
    "Delay (in milliseconds) after the most recent key-stroke "+
    "before auto-completing.");

define_variable("minibuffer_auto_complete_preferences", {});

define_variable("minibuffer_auto_complete_default", false,
    "Boolean specifying whether to auto-complete by default. "+
    "The user variable `minibuffer_auto_complete_preferences' "+
    "overrides this.");

var minibuffer_history_data = {};

/* FIXME: These should possibly be saved to disk somewhere */
define_variable("minibuffer_history_max_items", 100,
    "Maximum number of minibuffer history entries stored. Older "+
    "history entries are truncated after this limit is reached.");

define_variable("minibuffer_completion_rows", 8,
    "Number of minibuffer completions to display at one time.");

var atom_service = Cc["@mozilla.org/atom-service;1"].getService(Ci.nsIAtomService);

function completions_tree_view (minibuffer_state) {
    this.minibuffer_state = minibuffer_state;
}
completions_tree_view.prototype = {
    constructor: completions_tree_view,
    QueryInterface: XPCOMUtils.generateQI([Ci.nsITreeView]),
    get rowCount () {
        var c = this.minibuffer_state.completions;
        if (!c)
            return 0;
        return c.count;
    },
    getCellText: function (row,column) {
        var c = this.minibuffer_state.completions;
        if (row >= c.count)
            return null;
        if (column.index == 0)
            return c.get_string(row);
        return c.get_description(row);
    },
    setTree: function (treebox) { this.treeBox = treebox; },
    isContainer: function (row) { return false; },
    isSeparator: function (row) { return false; },
    isSorted: function () { return false; },
    getLevel: function (row) { return 0; },
    getImageSrc: function (row, col) {
        var c = this.minibuffer_state.completions;
        if (this.minibuffer_state.enable_icons &&
            col.index == 0)
        {
            return c.get_icon(row);
        }
        return null;
    },
    getRowProperties: function (row, props) {},
    getCellProperties: function (row, col, props) {
        if (col.index == 0)
            var a = atom_service.getAtom("completion-string");
        else
            a = atom_service.getAtom("completion-description");
        if (props)
            props.AppendElement(a);
        return a;
    },
    getColumnProperties: function (colid, col, props) {}
};


/* The parameter `args' specifies the arguments.  In addition, the
 * arguments for basic_minibuffer_state are also allowed.
 *
 * history:           [optional] specifies a string to identify the history list to use
 *
 * completer
 *
 * require_match
 *
 * default_completion  only used if require_match is set to true
 *
 * $valiator          [optional]
 *          specifies a function
 */
define_keywords("$keymap", "$history", "$validator",
                "$completer", "$require_match", "$default_completion",
                "$auto_complete", "$auto_complete_initial", "$auto_complete_conservative",
                "$auto_complete_delay", "$enable_icons",
                "$space_completes");
/* FIXME: support completing in another thread */
function text_entry_minibuffer_state (minibuffer) {
    keywords(arguments, $keymap = minibuffer_keymap,
             $enable_icons = false);

    basic_minibuffer_state.call(this, minibuffer, forward_keywords(arguments));

    let deferred = Promise.defer();
    this.deferred = deferred;
    this.promise = make_simple_cancelable(deferred);

    if (arguments.$history) {
        this.history = minibuffer_history_data[arguments.$history] =
            minibuffer_history_data[arguments.$history] || [];
        this.history_index = -1;
        this.saved_last_history_entry = null;
    }

    this.validator = arguments.$validator;

    if (arguments.$completer != null) {
        this.completer = arguments.$completer;
        let auto = arguments.$auto_complete;
        while (typeof auto == "string")
            auto = minibuffer_auto_complete_preferences[auto];
        if (auto == null)
            auto = minibuffer_auto_complete_default;
        this.auto_complete = auto;
        this.auto_complete_initial = !!arguments.$auto_complete_initial;
        this.auto_complete_conservative = !!arguments.$auto_complete_conservative;
        let delay = arguments.$auto_complete_delay;
        if (delay == null)
            delay = default_minibuffer_auto_complete_delay;
        this.auto_complete_delay = delay;
        this.completions = null;
        this.completions_valid = false;
        this.space_completes = !!arguments.$space_completes;
        if (this.space_completes)
            this.keymaps.push(minibuffer_space_completion_keymap);
        this.completions_timer_ID = null;
        this.completions_display_element = null;
        this.selected_completion_index = -1;
        this.require_match = !!arguments.$require_match;
        this.require_match_default = this.require_match;
        if (this.require_match)
            this.default_completion = arguments.$default_completion;
        this.enable_icons = arguments.$enable_icons;
    }
}
text_entry_minibuffer_state.prototype = {
    constructor: text_entry_minibuffer_state,
    __proto__: basic_minibuffer_state.prototype,
    load: function () {
        basic_minibuffer_state.prototype.load.call(this);
        var window = this.minibuffer.window;
        if (this.completer) {
            // Create completion display element if needed
            if (! this.completion_element) {
                /* FIXME: maybe use the dom_generator */
                var tree = create_XUL(window, "tree");
                var s = this;
                tree.addEventListener("select", function () {
                        s.selected_completion_index = s.completions_display_element.currentIndex;
                        s.handle_completion_selected();
                    }, true);
                tree.setAttribute("class", "completions");

                tree.setAttribute("rows", minibuffer_completion_rows);

                tree.setAttribute("collapsed", "true");

                tree.setAttribute("hidecolumnpicker", "true");
                tree.setAttribute("hideheader", "true");
                if (this.enable_icons)
                    tree.setAttribute("hasicons", "true");

                var treecols = create_XUL(window, "treecols");
                tree.appendChild(treecols);
                var treecol = create_XUL(window, "treecol");
                treecol.setAttribute("flex", "1");
                treecols.appendChild(treecol);
                treecol = create_XUL(window, "treecol");
                treecol.setAttribute("flex", "1");
                treecols.appendChild(treecol);
                tree.appendChild(create_XUL(window, "treechildren"));

                this.minibuffer.insert_before(tree);
                tree.view = new completions_tree_view(this);
                this.completions_display_element = tree;

                // This is the initial loading of this minibuffer state.
                // If this.auto_complete_initial is true, generate
                // completions.
                if (this.auto_complete_initial)
                    this.handle_input();
            }
            this.update_completions_display();
        }
    },

    unload: function () {
        if (this.completions_display_element)
            this.completions_display_element.setAttribute("collapsed", "true");
        basic_minibuffer_state.prototype.unload.call(this);
    },

    destroy: function () {
        if (this.completions)
            this.completions.destroy();
        delete this.completions;
        if (this.completions_cont)
            this.completions_cont.cancel();
        delete this.completions_cont;

        var el = this.completions_display_element;
        if (el) {
            el.parentNode.removeChild(el);
            this.completions_display_element = null;
        }
        if (this.promise)
            this.promise.cancel();
        basic_minibuffer_state.prototype.destroy.call(this);
    },

    handle_input: function () {
        if (! this.completer)
            return;
        this.completions_valid = false;
        if (! this.auto_complete)
            return;
        var s = this;
        var window = this.minibuffer.window;
        if (this.auto_complete_delay > 0) {
            if (this.completions_timer_ID != null)
                window.clearTimeout(this.completions_timer_ID);
            this.completions_timer_ID = window.setTimeout(
                function () {
                    s.completions_timer_ID = null;
                    s.update_completions(true /* auto */, true /* update completions display */);
                }, this.auto_complete_delay);
        } else {
            s.update_completions(true /* auto */, true /* update completions display */);
        }
    },

    update_completions_display: function () {
        var m = this.minibuffer;
        if (m.current_state == this) {
            if (this.completions && this.completions.count > 0) {
                this.completions_display_element.view = this.completions_display_element.view;
                this.completions_display_element.setAttribute("collapsed", "false");
                this.completions_display_element.currentIndex = this.selected_completion_index;
                var max_display = this.completions_display_element.treeBoxObject.getPageLength();
                var mid_point = Math.floor(max_display / 2);
                if (this.completions.count - this.selected_completion_index <= mid_point)
                    var pos = this.completions.count - max_display;
                else
                    pos = Math.max(0, this.selected_completion_index - mid_point);
                this.completions_display_element.treeBoxObject.scrollToRow(pos);
            } else {
                this.completions_display_element.setAttribute("collapsed", "true");
            }
        }
    },

    /* If auto is true, this update is due to auto completion, rather
     * than specifically requested. */
    update_completions: function (auto, update_display) {
        var window = this.minibuffer.window;
        if (this.completions_timer_ID != null) {
            window.clearTimeout(this.completions_timer_ID);
            this.completions_timer_ID = null;
        }
        var m = this.minibuffer;
        if (this.completions_cont) {
            this.completions_cont.cancel();
            this.completions_cont = null;
        }
        if (m._selection_start > 0 || ! auto || ! this.auto_complete_conservative)
            var c = this.completer.complete(m._input_text, m._selection_start);
        if (is_coroutine(c)) {
            var s = this;
            var already_done = false;
            this.completions_cont = spawn(function () {
                try {
                    var x = yield c;
                } catch (e) {
                    handle_interactive_error(window, e);
                } finally {
                    s.completions_cont = null;
                    already_done = true;
                }
                s.update_completions_done(x, update_display);
            }());
            // In case the completer actually already finished
            if (already_done)
                this.completions_cont = null;
        } else
            this.update_completions_done(c, update_display);
    },

    update_completions_done: function (c, update_display) {
        /* The completer should return undefined if completion was not
         * attempted due to auto being true.  Otherwise, it can return
         * null to indicate no completions. */
        if (this.completions)
            this.completions.destroy();

        this.completions = c;
        this.completions_valid = true;
        this.applied_common_prefix = false;

        this.require_match = this.completer.require_match;
        if (this.require_match == null)
            this.require_match = this.require_match_default;

        if (c && c.count > 0) {
            var i = -1;
            if (this.require_match) {
                if (c.count == 1)
                    i = 0;
                else if (c.default_completion != null)
                    i = c.default_completion;
                else if (this.default_completion)
                    i = this.completions.index_of(this.default_completion);
            }
            this.selected_completion_index = i;
        }

        if (update_display)
            this.update_completions_display();
    },

    select_completion: function (i) {
        this.selected_completion_index = i;
        this.completions_display_element.currentIndex = i;
        if (i >= 0)
            this.completions_display_element.treeBoxObject.ensureRowIsVisible(i);
        this.handle_completion_selected();
    },

    handle_completion_selected: function () {
        /**
         * When a completion is selected, apply it to the input text
         * if a match is not "required"; otherwise, the completion is
         * only displayed.
         */
        var i = this.selected_completion_index;
        var m = this.minibuffer;
        var c = this.completions;

        if (this.completions_valid && c && !this.require_match && i >= 0 && i < c.count)
            m.set_input_state(c.get_input_state(i));
    }
};

function minibuffer_complete (window, count) {
    var m = window.minibuffer;
    var s = m.current_state;
    if (! (s instanceof text_entry_minibuffer_state))
        throw new Error("Invalid minibuffer state");
    if (! s.completer)
        return;
    var just_completed_manually = false;
    if (! s.completions_valid || s.completions === undefined) {
        if (s.completions_timer_ID == null)
            just_completed_manually = true;
        //XXX: may need to use ignore_input_events here
        s.update_completions(false /* not auto */, true /* update completions display */);

        // If the completer is a coroutine, nothing we can do here
        if (! s.completions_valid)
            return;
    }

    var c = s.completions;

    if (! c || c.count == 0)
        return;

    var e = s.completions_display_element;
    var new_index = -1;
    var common_prefix;

    if (count == 1 && ! s.applied_common_prefix &&
        (common_prefix = c.common_prefix_input_state))
    {
        //XXX: may need to use ignore_input_events here
        m.set_input_state(common_prefix);
        s.applied_common_prefix = true;
    } else if (!just_completed_manually) {
        if (e.currentIndex != -1) {
            new_index = (e.currentIndex + count) % c.count;
            if (new_index < 0)
                new_index += c.count;
        } else {
            new_index = (count - 1) % c.count;
            if (new_index < 0)
                new_index += c.count;
        }
    }

    if (new_index != -1) {
       try {
            m.ignore_input_events = true;
            s.select_completion(new_index);
        } finally {
            m.ignore_input_events = false;
        }
    }
}
interactive("minibuffer-complete", null,
    function (I) { minibuffer_complete(I.window, I.p); });
interactive("minibuffer-complete-previous", null,
    function (I) { minibuffer_complete(I.window, -I.p); });

function exit_minibuffer (window) {
    var m = window.minibuffer;
    var s = m.current_state;
    if (! (s instanceof text_entry_minibuffer_state))
        throw new Error("Invalid minibuffer state");

    var val = m._input_text;

    if (s.validator != null && ! s.validator(val, m))
        return;

    var match = null;

    if (s.completer && s.require_match) {
        if (! s.completions_valid || s.completions === undefined)
            s.update_completions(false /* not conservative */, false /* don't update */);

        let c = s.completions;
        let i = s.selected_completion_index;
        if (c != null && i >= 0 && i < c.count) {
            match = c.get_value(i);
        } else {
            m.message("No match");
            return;
        }
    }

    if (s.history) {
        s.history.push(val);
        if (s.history.length > minibuffer_history_max_items)
            s.history.splice(0, s.history.length - minibuffer_history_max_items);
    }
    var deferred = s.deferred;
    if (deferred) {
        if (s.require_match) {
            deferred.resolve(match);
        }
        else {
            deferred.resolve(val);
        }
    }
    m.pop_state();
}
interactive("exit-minibuffer", null,
    function (I) { exit_minibuffer(I.window); });

function minibuffer_history_next (window, count) {
    var m = window.minibuffer;
    var s = m.current_state;
    if (! (s instanceof text_entry_minibuffer_state))
        throw new Error("Invalid minibuffer state");
    if (! s.history || s.history.length == 0)
        throw interactive_error("No history available.");
    if (count == 0)
        return;
    var index = s.history_index;
    if (count > 0 && index == -1)
        throw interactive_error("End of history; no next item");
    else if (count < 0 && index == 0)
        throw interactive_error("Beginning of history; no preceding item");
    if (index == -1) {
        s.saved_last_history_entry = m._input_text;
        index = s.history.length + count;
    } else
        index = index + count;

    if (index < 0)
        index = 0;

    m._restore_normal_state();
    if (index >= s.history.length) {
        index = -1;
        m._input_text = s.saved_last_history_entry;
    } else {
        m._input_text = s.history[index];
    }
    s.history_index = index;
    m._set_selection();
    s.handle_input();
}
interactive("minibuffer-history-next", null,
    function (I) { minibuffer_history_next(I.window, I.p); });
interactive("minibuffer-history-previous", null,
    function (I) { minibuffer_history_next(I.window, -I.p); });

// Define the asynchronous minibuffer.read function
minibuffer.prototype.read = function () {
    var s = new text_entry_minibuffer_state(this, forward_keywords(arguments));
    this.push_state(s);
    yield co_return(yield s.promise);
};

minibuffer.prototype.read_command = function () {
    keywords(
        arguments,
        $prompt = "Command", $history = "command",
        $completer = new prefix_completer(
            $completions = function (visitor) {
                for (let [k,v] in Iterator(interactive_commands)) {
                    visitor(v);
                }
            },
            $get_string = function (x) x.name,
            $get_description = function (x) x.shortdoc || "",
            $get_value = function (x) x.name),
        $require_match,
        $space_completes);
    var result = yield this.read(forward_keywords(arguments));
    yield co_return(result);
};

minibuffer.prototype.read_user_variable = function () {
    keywords(
        arguments,
        $prompt = "User variable", $history = "user_variable",
        $completer = new prefix_completer(
            $completions = function (visitor) {
                for (var i in user_variables) visitor(i);
            },
            $get_string = function (x) x,
            $get_description = function (x) user_variables[x].shortdoc || "",
            $get_value = function (x) x),
        $require_match,
        $space_completes);
    var result = yield this.read(forward_keywords(arguments));
    yield co_return(result);
};

minibuffer.prototype.read_preference = function () {
    keywords(arguments,
             $prompt = "Preference:", $history = "preference",
             $completer = new prefix_completer(
                 $completions = preferences.getBranch(null).getChildList("", {}),
                 $get_description = function (pref) {
                     let default_value = get_default_pref(pref);
                     let value = get_pref(pref);
                     if (value == default_value)
                         value = null;
                     let type;
                     switch (preferences.getBranch(null).getPrefType(pref)) {
                     case Ci.nsIPrefBranch.PREF_STRING:
                         type = "string";
                         break;
                     case Ci.nsIPrefBranch.PREF_INT:
                         type = "int";
                         break;
                     case Ci.nsIPrefBranch.PREF_BOOL:
                         type = "boolean";
                         break;
                     }
                     let out = type + ":";
                     if (value != null)
                         out += " " + pretty_print_value(value);
                     if (default_value != null)
                         out += " (" + pretty_print_value(default_value) + ")";
                     return out;
                 }),
             $require_match,
             $space_completes);
    var result = yield this.read(forward_keywords(arguments));
    yield co_return(result);
};


define_keywords("$object");
minibuffer.prototype.read_object_property = function () {
    keywords(arguments,
             $prompt = "Property:");
    var o = arguments.$object || {};
    var result = yield this.read(
        $prompt = arguments.$prompt,
        $completer = new prefix_completer(
            $completions = function (push) {
                for (var i in o)
                    push(i);
            }),
        $require_match,
        $space_completes);
    yield co_return(result);
};


provide("minibuffer-read");