1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
11 var CommandWidgets = Class("CommandWidgets", {
12 depends: ["statusline"],
14 init: function init() {
15 let s = "dactyl-statusline-field-";
17 overlay.overlayWindow(window, {
19 eventTarget: commandline
22 ["vbox", { id: config.ids.commandContainer, xmlns: "xul" },
23 ["vbox", { class: "dactyl-container", hidden: "false", collapsed: "true" },
24 ["iframe", { class: "dactyl-completions", id: "dactyl-completions-dactyl-commandline",
25 src: "dactyl://content/buffer.xhtml", contextmenu: "dactyl-contextmenu",
26 flex: "1", hidden: "false", collapsed: "false",
27 highlight: "Events", events: "mowEvents" }]],
29 ["stack", { orient: "horizontal", align: "stretch", class: "dactyl-container",
30 id: "dactyl-container", highlight: "CmdLine CmdCmdLine" },
31 ["textbox", { class: "plain", id: "dactyl-strut", flex: "1", crop: "end", collapsed: "true" }],
32 ["textbox", { class: "plain", id: "dactyl-mode", flex: "1", crop: "end" }],
33 ["hbox", { id: "dactyl-message-box" },
34 ["label", { class: "plain", id: "dactyl-message-pre", flex: "0", readonly: "true", highlight: "WarningMsg" }],
35 ["textbox", { class: "plain", id: "dactyl-message", flex: "1", readonly: "true" }]],
37 ["hbox", { id: "dactyl-commandline", hidden: "false", class: "dactyl-container", highlight: "Normal CmdNormal", collapsed: "true" },
38 ["label", { id: "dactyl-commandline-prompt", class: "dactyl-commandline-prompt plain", flex: "0", crop: "end", value: "", collapsed: "true" }],
39 ["textbox", { id: "dactyl-commandline-command", class: "dactyl-commandline-command plain", flex: "1", type: "input", timeout: "100",
40 highlight: "Events" }]]],
42 ["vbox", { class: "dactyl-container", hidden: "false", collapsed: "false", highlight: "CmdLine" },
43 ["textbox", { id: "dactyl-multiline-input", class: "plain", flex: "1", rows: "1", hidden: "false", collapsed: "true",
44 multiline: "true", highlight: "Normal Events", events: "multilineInputEvents" }]]],
46 ["stack", { id: "dactyl-statusline-stack", xmlns: "xul" },
47 ["hbox", { id: s + "commandline", hidden: "false", class: "dactyl-container", highlight: "Normal StatusNormal", collapsed: "true" },
48 ["label", { id: s + "commandline-prompt", class: "dactyl-commandline-prompt plain", flex: "0", crop: "end", value: "", collapsed: "true" }],
49 ["textbox", { id: s + "commandline-command", class: "dactyl-commandline-command plain", flex: "1", type: "text", timeout: "100",
50 highlight: "Events", }]]]],
53 ["toolbar", { id: statusline.statusBar.id, xmlns: "xul" },
54 ["vbox", { id: "dactyl-completions-" + s + "commandline-container", class: "dactyl-container", hidden: "false", collapsed: "true" },
55 ["iframe", { class: "dactyl-completions", id: "dactyl-completions-" + s + "commandline", src: "dactyl://content/buffer.xhtml",
56 contextmenu: "dactyl-contextmenu", flex: "1", hidden: "false", collapsed: "false", highlight: "Events",
57 events: "mowEvents" }]]]]
69 getGroup: function () options.get("guioptions").has("C") ? this.commandbar : this.statusbar,
70 getValue: function () this.command
75 defaultGroup: "Normal",
76 getGroup: function () this.commandbar,
77 getValue: function () options.get("guioptions").has("c")
82 test: function test(stack, prev) stack.pop && !isinstance(prev.main, modes.COMMAND_LINE),
83 id: "commandline-command",
84 get: function command_get(elem) {
85 // The long path is because of complications with the
86 // completion preview.
88 return elem.inputField.editor.rootElement.firstChild.textContent;
94 getElement: CommandWidgets.getEditor,
95 getGroup: function (value) this.activeGroup.commandline,
96 onChange: function command_onChange(elem, value) {
97 if (elem.inputField != dactyl.focusedElement)
99 elem.selectionStart = elem.value.length;
100 elem.selectionEnd = elem.value.length;
107 onVisibility: function command_onVisibility(elem, visible) {
115 id: "commandline-prompt",
116 defaultGroup: "CmdPrompt",
117 getGroup: function () this.activeGroup.commandline
122 defaultGroup: "Normal",
123 getElement: CommandWidgets.getEditor,
124 getGroup: function (value) {
125 if (this.command && !options.get("guioptions").has("M"))
126 return this.statusbar;
128 let statusElem = this.statusbar.message;
129 // Currently doesn't work as expected with <hbox> parent.
130 if (false && value && !value[2] && statusElem.editor && statusElem.editor.rootElement.scrollWidth > statusElem.scrollWidth)
131 return this.commandbar;
132 return this.activeGroup.mode;
138 defaultGroup: "WarningMsg",
139 getGroup: function () this.activeGroup.message
144 defaultGroup: "Normal",
145 getGroup: function () this.activeGroup.message,
146 getValue: function () this.message
151 defaultGroup: "ModeMsg",
152 getGroup: function (value) {
153 if (!options.get("guioptions").has("M"))
154 if (this.commandbar.container.clientHeight == 0 ||
155 value && !this.commandbar.commandline.collapsed)
156 return this.statusbar;
157 return this.commandbar;
160 this.updateVisibility();
162 this.initialized = true;
164 addElement: function addElement(obj) {
166 this.elements[obj.name] = obj;
168 function get(prefix, map, id) (obj.getElement || util.identity)(map[id] || document.getElementById(prefix + id));
170 this.active.__defineGetter__(obj.name, () => this.activeGroup[obj.name][obj.name]);
171 this.activeGroup.__defineGetter__(obj.name, () => this.getGroup(obj.name));
173 memoize(this.statusbar, obj.name, () => get("dactyl-statusline-field-", statusline.widgets, (obj.id || obj.name)));
174 memoize(this.commandbar, obj.name, () => get("dactyl-", {}, (obj.id || obj.name)));
176 if (!(obj.noValue || obj.getValue)) {
177 Object.defineProperty(this, obj.name, Modes.boundProperty({
180 get: function get_widgetValue() {
181 let elem = self.getGroup(obj.name, obj.value)[obj.name];
182 if (obj.value != null)
183 return [obj.value[0],
184 obj.get ? obj.get.call(this, elem) : elem.value]
185 .concat(obj.value.slice(2));
189 set: function set_widgetValue(val) {
190 if (val != null && !isArray(val))
191 val = [obj.defaultGroup || "", val];
194 [this.commandbar, this.statusbar].forEach(function (nodeSet) {
195 let elem = nodeSet[obj.name];
199 highlight.highlightNode(elem,
200 (val[0] != null ? val[0] : obj.defaultGroup)
201 .split(/\s/).filter(util.identity)
202 .map(g => g + " " + nodeSet.group + g)
206 obj.onChange.call(this, elem, val);
210 this.updateVisibility();
215 else if (obj.defaultGroup) {
216 [this.commandbar, this.statusbar].forEach(function (nodeSet) {
217 let elem = nodeSet[obj.name];
219 highlight.highlightNode(elem, obj.defaultGroup.split(/\s/)
220 .map(g => g + " " + nodeSet.group + g)
226 getGroup: function getgroup(name, value) {
227 if (!statusline.visible)
228 return this.commandbar;
229 return this.elements[name].getGroup.call(this, arguments.length > 1 ? value : this[name]);
232 updateVisibility: function updateVisibility() {
234 for (let elem in values(this.elements))
236 let value = elem.getValue ? elem.getValue.call(this)
237 : elem.noValue || this[elem.name];
239 let activeGroup = this.getGroup(elem.name, value);
240 for (let group in values([this.commandbar, this.statusbar])) {
241 let meth, node = group[elem.name];
242 let visible = (value && group === activeGroup);
243 if (node && !node.collapsed == !visible) {
245 node.collapsed = !visible;
246 if (elem.onVisibility)
247 elem.onVisibility.call(this, node, visible);
252 // Hack. Collapse hidden elements in the stack.
253 // Might possibly be better to use a deck and programmatically
254 // choose which element to select.
255 function check(node) {
256 if (DOM(node).style.display === "-moz-stack") {
257 let nodes = Array.filter(node.children, n => !n.collapsed && n.boxObject.height);
258 nodes.forEach((node, i) => {
259 node.style.opacity = (i == nodes.length - 1) ? "" : "0";
262 Array.forEach(node.children, check);
264 [this.commandbar.container, this.statusbar.container].forEach(check);
266 if (this.initialized && loaded.has("mow") && mow.visible)
270 active: Class.Memoize(Object),
271 activeGroup: Class.Memoize(Object),
272 commandbar: Class.Memoize(function () ({ group: "Cmd" })),
273 statusbar: Class.Memoize(function () ({ group: "Status" })),
275 _ready: function _ready(elem) {
276 return elem.contentDocument.documentURI === elem.getAttribute("src") &&
277 ["viewable", "complete"].indexOf(elem.contentDocument.readyState) >= 0;
280 _whenReady: function _whenReady(id, init) {
281 let elem = document.getElementById(id);
282 while (!this._ready(elem))
286 init.call(this, elem);
290 completionContainer: Class.Memoize(function () this.completionList.parentNode),
292 contextMenu: Class.Memoize(function () {
293 ["copy", "copylink", "selectall"].forEach(function (tail) {
294 // some host apps use "hostPrefixContext-copy" ids
295 let css = "menuitem[id$='ontext-" + tail + "']:not([id^=dactyl-])";
296 let style = DOM(css, document).style;
297 DOM("#dactyl-context-" + tail, document).css({
298 listStyleImage: style.listStyleImage,
299 MozImageRegion: style.MozImageRegion
302 return document.getElementById("dactyl-contextmenu");
305 multilineOutput: Class.Memoize(function () this._whenReady("dactyl-multiline-output",
307 highlight.highlightNode(elem.contentDocument.body, "MOW");
310 multilineInput: Class.Memoize(() => document.getElementById("dactyl-multiline-input")),
312 mowContainer: Class.Memoize(() => document.getElementById("dactyl-multiline-output-container"))
314 getEditor: function getEditor(elem) {
315 elem.inputField.QueryInterface(Ci.nsIDOMNSEditableElement);
320 var CommandMode = Class("CommandMode", {
321 init: function CM_init() {
322 this.keepCommand = userContext.hidden_option_command_afterimage;
325 get autocomplete() options["autocomplete"].length,
327 get command() this.widgets.command[1],
328 set command(val) this.widgets.command = val,
330 get prompt() this._open ? this.widgets.prompt : this._prompt,
333 this.widgets.prompt = val;
338 open: function CM_open(command) {
339 dactyl.assert(isinstance(this.mode, modes.COMMAND_LINE),
340 /*L*/"Not opening command line in non-command-line mode.",
343 this.messageCount = commandline.messageCount;
344 modes.push(this.mode, this.extendedMode, this.bound);
346 this.widgets.active.commandline.collapsed = false;
347 this.widgets.prompt = this.prompt;
348 this.widgets.command = command || "";
352 this.input = this.widgets.active.command.inputField;
354 this.history = CommandLine.History(this.input, this.historyKey, this);
357 this.completions = CommandLine.Completions(this.input, this);
359 if (this.completions && command && commandline.commandSession === this)
360 this.completions.autocompleteTimer.flush(true);
363 get active() this === commandline.commandSession,
365 get holdFocus() this.widgets.active.command.inputField,
367 get mappingSelf() this,
369 get widgets() commandline.widgets,
371 enter: function CM_enter(stack) {
372 commandline.commandSession = this;
373 if (stack.pop && commandline.command) {
374 this.onChange(commandline.command);
375 if (this.completions && stack.pop)
376 this.completions.complete(true, false);
380 leave: function CM_leave(stack) {
382 commandline.commandSession = null;
383 this.input.dactylKeyPress = undefined;
385 let waiting = this.accepted && this.completions && this.completions.waiting;
387 this.completions.onComplete = bind("onSubmit", this);
389 if (this.completions)
390 this.completions.cleanup();
395 commandline.hideCompletions();
397 modes.delay(function () {
398 if (!this.keepCommand || commandline.silent || commandline.quiet)
401 this[this.accepted ? "onSubmit" : "onCancel"](commandline.command);
402 if (commandline.messageCount === this.messageCount)
403 commandline.clearMessage();
409 input: function CM_onInput(event) {
410 if (this.completions) {
411 this.resetCompletions();
413 this.completions.autocompleteTimer.tell(false);
415 this.onChange(commandline.command);
417 keyup: function CM_onKeyUp(event) {
418 let key = DOM.Event.stringify(event);
419 if (/-?Tab>$/.test(key) && this.completions)
420 this.completions.tabTimer.flush();
426 onKeyPress: function CM_onKeyPress(events) {
427 if (this.completions)
428 this.completions.previewClear();
430 return true; /* Pass event */
433 onCancel: function (value) {},
435 onChange: function (value) {},
437 onHistory: function (value) {},
439 onSubmit: function (value) {},
441 resetCompletions: function CM_resetCompletions() {
442 if (this.completions)
443 this.completions.clear();
445 this.history.reset();
449 var CommandExMode = Class("CommandExMode", CommandMode, {
453 historyKey: "command",
455 prompt: ["Normal", ":"],
457 complete: function CEM_complete(context) {
459 context.fork("ex", 0, completion, "ex");
462 context.message = _("error.error", e);
466 onSubmit: function CEM_onSubmit(command) {
467 contexts.withContext({ file: /*L*/"[Command Line]", line: 1 },
468 function _onSubmit() {
469 io.withSavedValues(["readHeredoc"], function _onSubmit() {
470 this.readHeredoc = commandline.readHeredoc;
471 commands.repeat = command;
472 dactyl.execute(command);
478 var CommandPromptMode = Class("CommandPromptMode", CommandMode, {
479 init: function init(prompt, params) {
480 this.prompt = isArray(prompt) ? prompt : ["Question", prompt];
481 update(this, params);
482 init.supercall(this);
485 complete: function CPM_complete(context, ...args) {
487 context.forkapply("prompt", 0, this, "completer", args);
490 get mode() modes.PROMPT
494 * This class is used for prompting of user input and echoing of messages.
496 * It consists of a prompt and command field be sure to only create objects of
497 * this class when the chrome is ready.
499 var CommandLine = Module("commandline", {
500 init: function init() {
501 this._callbacks = {};
503 memoize(this, "_store", function () storage.newMap("command-history", { store: true, privateData: true }));
505 for (let name in values(["command", "search"]))
506 if (storage.exists("history-" + name)) {
507 let ary = storage.newArray("history-" + name, { store: true, privateData: true });
509 this._store.set(name, [v for ([k, v] in ary)]);
511 this._store.changed();
514 this._messageHistory = { //{{{
517 let max = options["messages"];
519 // resize if 'messages' has changed
520 if (this._messages.length > max)
521 this._messages = this._messages.splice(this._messages.length - max);
523 return this._messages;
526 get length() this._messages.length,
528 clear: function clear() {
532 filter: function filter(fn, self) {
533 this._messages = this._messages.filter(fn, self);
536 add: function add(message) {
540 if (this._messages.length >= options["messages"])
541 this._messages.shift();
543 this._messages.push(update({
544 timestamp: Date.now()
551 "browser.locationChange": function (webProgress, request, uri) {
557 * Determines whether the command line should be visible.
561 get commandVisible() !!this.commandSession,
564 * Ensure that the multiline input widget is the correct size.
566 _autosizeMultilineInputWidget: function _autosizeMultilineInputWidget() {
567 let lines = this.widgets.multilineInput.value.split("\n").length - 1;
569 this.widgets.multilineInput.setAttribute("rows", Math.max(lines, 1));
573 HL_ERRORMSG: "ErrorMsg",
574 HL_MODEMSG: "ModeMsg",
575 HL_MOREMSG: "MoreMsg",
576 HL_QUESTION: "Question",
577 HL_INFOMSG: "InfoMsg",
578 HL_WARNINGMSG: "WarningMsg",
581 FORCE_MULTILINE : 1 << 0,
582 FORCE_SINGLELINE : 1 << 1,
583 DISALLOW_MULTILINE : 1 << 2, // If an echo() should try to use the single line
584 // but output nothing when the MOW is open; when also
585 // FORCE_MULTILINE is given, FORCE_MULTILINE takes precedence
586 APPEND_TO_MESSAGES : 1 << 3, // Add the string to the message history.
587 ACTIVE_WINDOW : 1 << 4, // Only echo in active window.
589 get completionContext() this._completions.context,
592 get silent() this._silent,
595 this.quiet = this.quiet;
599 get quiet() this._quiet,
602 ["commandbar", "statusbar"].forEach(function (nodeSet) {
603 Array.forEach(this.widgets[nodeSet].commandline.children, function (node) {
604 node.style.opacity = this._quiet || this._silent ? "0" : "";
609 widgets: Class.Memoize(() => CommandWidgets()),
611 runSilently: function runSilently(func, self) {
612 this.withSavedValues(["silent"], function () {
618 get completionList() {
619 let node = this.widgets.active.commandline;
620 if (this.commandSession && this.commandSession.completionList)
621 node = document.getElementById(this.commandSession.completionList);
623 if (!node.completionList) {
624 let elem = document.getElementById("dactyl-completions-" + node.id);
625 util.waitFor(bind(this.widgets._ready, null, elem));
627 node.completionList = ItemList(elem);
628 node.completionList.isAboveMow = node.id ==
629 this.widgets.statusbar.commandline.id;
631 return node.completionList;
634 hideCompletions: function hideCompletions() {
635 for (let nodeSet in values([this.widgets.statusbar, this.widgets.commandbar]))
636 if (nodeSet.commandline.completionList)
637 nodeSet.commandline.completionList.visible = false;
640 _lastClearable: Modes.boundProperty(),
641 messages: Modes.boundProperty(),
643 multilineInputVisible: Modes.boundProperty({
644 set: function set_miwVisible(value) { this.widgets.multilineInput.collapsed = !value; }
648 if (this.commandVisible && this.widgets.command)
649 return commands.lastCommand = this.widgets.command[1];
650 return commands.lastCommand;
653 if (this.commandVisible && (modes.extended & modes.EX))
654 return this.widgets.command = val;
655 return commands.lastCommand = val;
658 clear: function clear(scroll) {
659 if (!scroll || Date.now() - this._lastEchoTime > 5000)
661 this._lastEchoTime = 0;
662 this.hiddenMessages = 0;
664 if (!this.commandSession) {
665 this.widgets.command = null;
666 this.hideCompletions();
669 if (modes.main == modes.OUTPUT_MULTILINE && !mow.isScrollable(1))
672 if (!modes.have(modes.OUTPUT_MULTILINE))
676 clearMessage: function clearMessage() {
677 if (this.widgets.message && this.widgets.message[1] === this._lastClearable) {
678 this.widgets.message = null;
679 this.hiddenMessages = 0;
684 * Displays the multi-line output of a command, preceded by the last
685 * executed ex command string.
687 * @param {object} xml The output as a JSON XML object.
689 commandOutput: function commandOutput(xml) {
691 this.echo(xml, this.HIGHLIGHT_NORMAL, this.FORCE_MULTILINE);
693 this.echo([["div", { xmlns: "html" }, ":" + this.command], "\n", xml],
694 this.HIGHLIGHT_NORMAL, this.FORCE_MULTILINE);
699 * Hides the command line, and shows any status messages that
702 hide: function hide() {
703 this.widgets.command = null;
707 * Display a message in the command-line area.
709 * @param {string} str
710 * @param {string} highlightGroup
711 * @param {boolean} forceSingle If provided, don't let over-long
712 * messages move to the MOW.
714 _echoLine: function echoLine(str, highlightGroup, forceSingle, silent) {
715 this.widgets.message = str ? [highlightGroup, str, forceSingle] : null;
717 dactyl.triggerObserver("echoLine", str, highlightGroup, null, forceSingle);
719 if (!this.commandVisible)
722 let field = this.widgets.active.message.inputField;
723 if (field.value && !forceSingle && field.editor.rootElement.scrollWidth > field.scrollWidth) {
724 this.widgets.message = null;
725 mow.echo(["span", { highlight: "Message" }, str], highlightGroup, true);
730 get hiddenMessages() this._hiddenMessages,
731 set hiddenMessages(val) {
732 this._hiddenMessages = val;
734 this.widgets["message-pre"] = _("commandline.moreMessages", val) + " ";
736 this.widgets["message-pre"] = null;
742 * Output the given string onto the command line. With no flags, the
743 * message will be shown in the status line if it's short enough to
744 * fit, and contains no new lines, and isn't XML. Otherwise, it will be
747 * @param {string} str
748 * @param {string} highlightGroup The Highlight group for the
751 * @param {number} flags Changes the behavior as follows:
752 * commandline.APPEND_TO_MESSAGES - Causes message to be added to the
753 * messages history, and shown by :messages.
754 * commandline.FORCE_SINGLELINE - Forbids the command from being
755 * pushed to the MOW if it's too long or of there are already
756 * status messages being shown.
757 * commandline.DISALLOW_MULTILINE - Cancels the operation if the MOW
758 * is already visible.
759 * commandline.FORCE_MULTILINE - Forces the message to appear in
763 echo: function echo(data, highlightGroup, flags) {
764 // dactyl.echo uses different order of flags as it omits the highlight group, change commandline.echo argument order? --mst
765 if (this._silent || !this.widgets)
770 highlightGroup = highlightGroup || this.HL_NORMAL;
772 let appendToMessages = (data) => {
773 let message = isObject(data) && !DOM.isJSONXML(data) ? data : { message: data };
775 // Make sure the memoized message property is an instance property.
777 this._messageHistory.add(update({ highlight: highlightGroup }, message));
778 return message.message;
781 if (flags & this.APPEND_TO_MESSAGES)
782 data = appendToMessages(data);
784 if ((flags & this.ACTIVE_WINDOW) && window != overlay.activeWindow)
787 if ((flags & this.DISALLOW_MULTILINE) && !this.widgets.mowContainer.collapsed)
790 let forceSingle = flags & (this.FORCE_SINGLELINE | this.DISALLOW_MULTILINE);
791 let action = this._echoLine;
793 if ((flags & this.FORCE_MULTILINE) || (/\n/.test(data) || !isinstance(data, [_, "String"])) && !(flags & this.FORCE_SINGLELINE))
794 action = mow.bound.echo;
796 let checkSingleLine = () => action == this._echoLine;
799 this._lastEcho = null;
800 this.hiddenMessages = 0;
804 if (checkSingleLine() && !this.widgets.mowContainer.collapsed) {
805 highlightGroup += " Message";
806 action = mow.bound.echo;
808 else if (!checkSingleLine() && this.widgets.mowContainer.collapsed) {
809 if (this._lastEcho && this.widgets.message && this.widgets.message[1] == this._lastEcho.msg) {
810 if (!(this._lastEcho.flags & this.APPEND_TO_MESSAGES))
811 appendToMessages(this._lastEcho.data);
814 ["span", { highlight: "Message" },
815 ["span", { highlight: "WarningMsg" },
816 _("commandline.moreMessages", this.hiddenMessages + 1) + " "],
818 this.widgets.message[0], true);
820 this.hiddenMessages = 0;
823 else if (this._lastEcho && this.widgets.message && this.widgets.message[1] == this._lastEcho.msg) {
824 if (!(this._lastEcho.flags & this.APPEND_TO_MESSAGES))
825 appendToMessages(this._lastEcho.data);
826 if (checkSingleLine() && !(flags & this.APPEND_TO_MESSAGES))
827 appendToMessages(data);
829 flags |= this.APPEND_TO_MESSAGES;
830 this.hiddenMessages++;
832 this._lastEcho = checkSingleLine() && { flags: flags, msg: data, data: arguments[0] };
835 this._lastClearable = action === this._echoLine && String(data);
836 this._lastEchoTime = (flags & this.FORCE_SINGLELINE) && Date.now();
839 action.call(this, data, highlightGroup, checkSingleLine());
844 * Prompt the user. Sets modes.main to COMMAND_LINE, which the user may
845 * pop at any time to close the prompt.
847 * @param {string} prompt The input prompt to use.
848 * @param {Object} extra
849 * @... {function} onChange - A function to be called with the current
850 * input every time it changes.
851 * @... {function(CompletionContext)} completer - A completion function
852 * for the user's input.
853 * @... {string} promptHighlight - The HighlightGroup used for the
854 * prompt. @default "Question"
855 * @... {string} default - The initial value that will be returned
856 * if the user presses <CR> straightaway. @default ""
858 input: promises.withCallbacks(function _input([callback, reject], prompt, extra={}, thing={}) {
861 [callback, extra] = [extra, thing];
863 CommandPromptMode(prompt, update({ onSubmit: callback, onCancel: reject }, extra)).open();
866 readHeredoc: function readHeredoc(end) {
867 return util.waitFor(commandline.inputMultiline(end));
871 * Get a multi-line input from a user, up to but not including the line
872 * which matches the given regular expression. Then execute the
873 * callback with that string as a parameter.
875 * @param {string} end
876 * @returns {Promise<string>}
878 // FIXME: Buggy, especially when pasting.
879 inputMultiline: promises.withCallbacks(function inputMultiline([callback], end) {
880 let cmd = this.command;
882 end: "\n" + end + "\n",
886 modes.push(modes.INPUT_MULTILINE, null, {
888 leave: function leave() {
896 this._echoLine(cmd, this.HL_NORMAL);
898 // save the arguments, they are needed in the event handler onKeyPress
900 this.multilineInputVisible = true;
901 this.widgets.multilineInput.value = "";
902 this._autosizeMultilineInputWidget();
904 this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10);
907 get commandMode() this.commandSession && isinstance(modes.main, modes.COMMAND_LINE),
910 iter(CommandMode.prototype.events).map(
911 ([event, handler]) => [
912 event, function (event) {
913 if (this.commandMode)
914 handler.call(this.commandSession, event);
918 focus: function onFocus(event) {
919 if (!this.commandSession
920 && event.originalTarget === this.widgets.active.command.inputField) {
928 get mowEvents() mow.events,
931 * Multiline input events, they will come straight from
932 * #dactyl-multiline-input in the XUL.
934 * @param {Event} event
936 multilineInputEvents: {
937 blur: function onBlur(event) {
938 if (modes.main == modes.INPUT_MULTILINE)
939 this.timeout(function () {
940 dactyl.focus(this.widgets.multilineInput.inputField);
943 input: function onInput(event) {
944 this._autosizeMultilineInputWidget();
948 updateOutputHeight: deprecated("mow.resize", function updateOutputHeight(open, extra) mow.resize(open, extra)),
950 withOutputToString: function withOutputToString(fn, self, ...args) {
951 dactyl.registerObserver("echoLine", observe, true);
952 dactyl.registerObserver("echoMultiline", observe, true);
955 function observe(str, highlight, dom) {
956 output.push(dom && !isString(str) ? dom : str);
959 this.savingOutput = true;
960 dactyl.trapErrors.apply(dactyl, [fn, self].concat(args));
961 this.savingOutput = false;
962 return output.map(elem => elem instanceof Node ? DOM.stringify(elem) : elem)
967 * A class for managing the history of an input field.
969 * @param {HTMLInputElement} inputField
970 * @param {string} mode The mode for which we need history.
972 History: Class("History", {
973 init: function init(inputField, mode, session) {
975 this.input = inputField;
977 this.session = session;
979 get store() commandline._store.get(this.mode, []),
980 set store(ary) { commandline._store.set(this.mode, ary); },
982 * Reset the history index to the first entry.
984 reset: function reset() {
988 * Save the last entry to the permanent store. All duplicate entries
989 * are removed and the list is truncated, if necessary.
991 save: function save() {
992 if (events.feedingKeys)
995 let str = this.input.value;
996 if (/^\s*$/.test(str))
999 let privateData = this.checkPrivate(str);
1000 if (privateData == "never-save")
1003 let store = Array.filter(this.store, line => (line.value || line) != str);
1005 () => store.push({ value: str, timestamp: Date.now() * 1000, privateData: privateData }));
1006 this.store = store.slice(Math.max(0, store.length - options["history"]));
1009 * @property {function} Returns whether a data item should be
1010 * considered private.
1012 checkPrivate: function checkPrivate(str) {
1013 // Not really the ideal place for this check.
1014 if (this.mode == "command")
1015 return commands.hasPrivateData(str);
1019 * Replace the current input field value.
1021 * @param {string} val The new value.
1023 replace: function replace(val) {
1024 editor.withSavedValues(["skipSave"], function () {
1025 editor.skipSave = true;
1027 this.input.dactylKeyPress = undefined;
1028 if (this.completions)
1029 this.completions.previewClear();
1030 this.input.value = val;
1031 this.session.onHistory(val);
1036 * Move forward or backward in history.
1038 * @param {boolean} backward Direction to move.
1039 * @param {boolean} matchCurrent Search for matches starting
1040 * with the current input value.
1042 select: function select(backward, matchCurrent) {
1043 // always reset the tab completion if we use up/down keys
1044 if (this.session.completions)
1045 this.session.completions.reset();
1047 let diff = backward ? -1 : 1;
1049 if (this.index == null) {
1050 this.original = this.input.value;
1051 this.index = this.store.length;
1054 // search the history for the first item matching the current
1055 // command-line string
1058 if (this.index < 0 || this.index > this.store.length) {
1059 this.index = Math.constrain(this.index, 0, this.store.length);
1061 // I don't know why this kludge is needed. It
1062 // prevents the caret from moving to the end of
1064 if (this.input.value == "") {
1065 this.input.value = " ";
1066 this.input.value = "";
1071 let hist = this.store[this.index];
1072 // user pressed DOWN when there is no newer history item
1074 hist = this.original;
1076 hist = (hist.value || hist);
1078 if (!matchCurrent || hist.substr(0, this.original.length) == this.original) {
1087 * A class for tab completions on an input field.
1089 * @param {Object} input
1091 Completions: Class("Completions", {
1100 init: function init(input, session) {
1103 this.context = CompletionContext(input.QueryInterface(Ci.nsIDOMNSEditableElement).editor);
1104 this.context.onUpdate = function onUpdate() { self.asyncUpdate(this); };
1106 this.editor = input.editor;
1108 this.session = session;
1110 this.wildmode = options.get("wildmode");
1111 this.wildtypes = this.wildmode.value;
1113 this.itemList = commandline.completionList;
1114 this.itemList.open(this.context);
1116 dactyl.registerObserver("events.doneFeeding", this.bound.onDoneFeeding, true);
1118 this.autocompleteTimer = Timer(200, 500, function autocompleteTell(tabPressed) {
1119 if (events.feedingKeys && !tabPressed)
1120 this.ignoredCount++;
1121 else if (this.session.autocomplete) {
1122 this.itemList.visible = true;
1123 this.complete(true, false);
1127 this.tabTimer = Timer(0, 0, function tabTell(event) {
1128 let tabCount = this.tabCount;
1130 this.tab(tabCount, event.altKey && options["altwildmode"]);
1141 onDoneFeeding: function onDoneFeeding() {
1142 if (this.ignoredCount)
1143 this.autocompleteTimer.flush(true);
1144 this.ignoredCount = 0;
1150 onTab: function onTab(event) {
1151 this.tabCount += event.shiftKey ? -1 : 1;
1152 this.tabTimer.tell(event);
1155 get activeContexts() this.context.contextList
1156 .filter(c => c.items.length || c.incomplete),
1159 * Returns the current completion string relative to the
1160 * offset of the currently selected context.
1163 let offset = this.selected ? this.selected[0].offset : this.start;
1164 return commandline.command.slice(offset, this.caret);
1168 * Updates the input field from *offset* to {@link #caret}
1169 * with the value *value*. Afterward, the caret is moved
1170 * just after the end of the updated text.
1172 * @param {number} offset The offset in the original input
1173 * string at which to insert *value*.
1174 * @param {string} value The value to insert.
1176 setCompletion: function setCompletion(offset, value) {
1177 editor.withSavedValues(["skipSave"], function () {
1178 editor.skipSave = true;
1179 this.previewClear();
1182 var [input, caret] = [this.originalValue, this.originalCaret];
1184 input = this.getCompletion(offset, value);
1185 caret = offset + value.length;
1188 // Change the completion text.
1189 // The second line is a hack to deal with some substring
1190 // preview corner cases.
1191 commandline.widgets.active.command.value = input;
1192 this.editor.selection.focusNode.textContent = input;
1195 this._caret = this.caret;
1197 this.input.dactylKeyPress = undefined;
1202 * For a given offset and completion string, returns the
1203 * full input value after selecting that item.
1205 * @param {number} offset The offset at which to insert the
1207 * @param {string} value The value to insert.
1208 * @returns {string};
1210 getCompletion: function getCompletion(offset, value) {
1211 return this.originalValue.substr(0, offset)
1213 + this.originalValue.substr(this.originalCaret);
1216 get selected() this.itemList.selected,
1217 set selected(tuple) {
1218 if (!array.equals(tuple || [],
1219 this.itemList.selected || []))
1220 this.itemList.select(tuple);
1223 this.setCompletion(null);
1225 let [ctxt, idx] = tuple;
1226 this.setCompletion(ctxt.offset, ctxt.items[idx].result);
1230 get caret() this.editor.selection.getRangeAt(0).startOffset,
1232 this.editor.selection.collapse(this.editor.rootElement.firstChild, offset);
1235 get start() this.context.allItems.start,
1237 get items() this.context.allItems.items,
1239 get substring() this.context.longestAllSubstring,
1241 get wildtype() this.wildtypes[this.wildIndex] || "",
1244 * Cleanup resources used by this completion session. This
1245 * instance should not be used again once this method is
1248 cleanup: function cleanup() {
1249 dactyl.unregisterObserver("events.doneFeeding", this.bound.onDoneFeeding);
1250 this.previewClear();
1252 this.tabTimer.reset();
1253 this.autocompleteTimer.reset();
1254 if (!this.onComplete)
1255 this.context.cancelAll();
1257 this.itemList.visible = false;
1258 this.input.dactylKeyPress = undefined;
1259 this.hasQuit = true;
1263 * Run the completer.
1265 * @param {boolean} show Passed to {@link #reset}.
1266 * @param {boolean} tabPressed Should be set to true if, and
1267 * only if, this function is being called in response
1270 complete: function complete(show, tabPressed) {
1271 this.session.ignoredCount = 0;
1273 this.waiting = null;
1274 this.context.reset();
1275 this.context.tabPressed = tabPressed;
1277 this.session.complete(this.context);
1278 if (!this.session.active)
1281 this.reset(show, tabPressed);
1283 this._caret = this.caret;
1287 * Clear any preview string and cancel any pending
1288 * asynchronous context. Called when there is further input
1291 clear: function clear() {
1292 this.context.cancelAll();
1293 this.wildIndex = -1;
1294 this.previewClear();
1298 * Saves the current input state. To be called before an
1299 * item is selected in a new set of completion responses.
1302 saveInput: function saveInput() {
1303 this.originalValue = this.context.value;
1304 this.originalCaret = this.caret;
1308 * Resets the completion state.
1310 * @param {boolean} show If true and options allow the
1311 * completion list to be shown, show it.
1313 reset: function reset(show) {
1314 this.waiting = null;
1315 this.wildIndex = -1;
1320 this.itemList.update();
1321 this.context.updateAsync = true;
1322 if (this.haveType("list"))
1323 this.itemList.visible = true;
1331 * Calls when an asynchronous completion context has new
1332 * results to return.
1334 * @param {CompletionContext} context The changed context.
1337 asyncUpdate: function asyncUpdate(context) {
1339 let item = this.getItem(this.waiting);
1340 if (item && this.waiting && this.onComplete) {
1341 util.trapErrors("onComplete", this,
1342 this.getCompletion(this.waiting[0].offset,
1344 this.waiting = null;
1345 this.context.cancelAll();
1350 let value = this.editor.selection.focusNode.textContent;
1353 if (this.itemList.visible)
1354 this.itemList.updateContext(context);
1356 if (this.waiting && this.waiting[0] == context)
1357 this.select(this.waiting);
1358 else if (!this.waiting) {
1359 let cursor = this.selected;
1360 if (cursor && cursor[0] == context) {
1361 let item = this.getItem(cursor);
1362 if (!item || this.completion != item.result)
1363 this.itemList.select(null);
1371 * Returns true if the currently selected 'wildmode' index
1372 * has the given completion type.
1374 haveType: function haveType(type)
1375 this.wildmode.checkHas(this.wildtype, type == "first" ? "" : type),
1378 * Returns the completion item for the given selection
1381 * @param {[CompletionContext,number]} tuple The spec of the
1383 * @default {@link #selected}
1386 getItem: function getItem(tuple=this.selected)
1387 tuple && tuple[0] && tuple[0].items[tuple[1]],
1390 * Returns a tuple representing the next item, at the given
1391 * *offset*, from *tuple*.
1393 * @param {[CompletionContext,number]} tuple The offset from
1395 * @default {@link #selected}
1396 * @param {number} offset The positive or negative offset to
1399 * @param {boolean} noWrap If true, and the search would
1400 * wrap, return null.
1402 nextItem: function nextItem(tuple, offset, noWrap) {
1403 if (tuple === undefined)
1404 tuple = this.selected;
1406 return this.itemList.getRelativeItem(offset || 1, tuple, noWrap);
1410 * The last previewed substring.
1416 * Displays a preview of the text provided by the next <Tab>
1417 * press if the current input is an anchored substring of
1420 preview: function preview() {
1421 this.previewClear();
1422 if (this.wildIndex < 0 || this.caret < this.input.value.length
1423 || !this.activeContexts.length || this.waiting)
1427 switch (this.wildtype.replace(/.*:/, "")) {
1429 var cursor = this.nextItem(null);
1432 if (this.items.length > 1) {
1433 substring = this.substring;
1438 cursor = this.nextItem();
1442 substring = this.getItem(cursor).result;
1444 // Don't show 1-character substrings unless we've just hit backspace
1445 if (substring.length < 2 && this.lastSubstring.indexOf(substring))
1448 this.lastSubstring = substring;
1450 let value = this.completion;
1451 if (util.compareIgnoreCase(value, substring.substr(0, value.length)))
1454 substring = substring.substr(value.length);
1455 this.removeSubstring = substring;
1457 let node = DOM.fromJSON(["span", { highlight: "Preview" }, substring],
1460 this.withSavedValues(["caret"], function () {
1461 this.editor.insertNode(node, this.editor.rootElement, 1);
1466 * Clears the currently displayed next-<Tab> preview string.
1468 previewClear: function previewClear() {
1469 let node = this.editor.rootElement.firstChild;
1470 if (node && node.nextSibling) {
1472 DOM(node.nextSibling).remove();
1475 node.nextSibling.textContent = "";
1478 else if (this.removeSubstring) {
1479 let str = this.removeSubstring;
1480 let cmd = commandline.widgets.active.command.value;
1481 if (cmd.substr(cmd.length - str.length) == str)
1482 commandline.widgets.active.command.value = cmd.substr(0, cmd.length - str.length);
1484 delete this.removeSubstring;
1488 * Selects a completion based on the value of *idx*.
1490 * @param {[CompletionContext,number]|const object} The
1491 * (context,index) tuple of the item to select, or an
1492 * offset constant from this object.
1493 * @param {number} count When given an offset constant,
1494 * select *count* units.
1496 * @param {boolean} fromTab If true, this function was
1497 * called by {@link #tab}.
1501 select: function select(idx, count=1, fromTab=false) {
1505 idx = this.nextItem(this.waiting || this.selected,
1506 idx == this.UP ? -count : count,
1511 case this.CTXT_DOWN:
1512 let groups = this.itemList.activeGroups;
1513 let i = Math.max(0, groups.indexOf(this.itemList.selectedGroup));
1515 i += idx == this.CTXT_DOWN ? 1 : -1;
1521 idx = [groups[i].context, 0];
1525 case this.PAGE_DOWN:
1526 idx = this.itemList.getRelativePage(idx == this.PAGE_DOWN ? 1 : -1);
1538 this.wildIndex = this.wildtypes.length - 1;
1540 if (idx && idx[1] >= idx[0].items.length) {
1541 if (!idx[0].incomplete)
1542 this.waiting = null;
1545 statusline.progress = _("completion.waitingForResults");
1550 this.waiting = null;
1552 this.itemList.select(idx, null, position);
1553 this.selected = idx;
1557 if (this.selected == null)
1558 statusline.progress = "";
1560 statusline.progress = _("completion.matchIndex",
1561 this.itemList.getOffset(idx),
1562 this.itemList.itemCount);
1566 * Selects a completion result based on the 'wildmode'
1567 * option, or the value of the *wildmode* parameter.
1569 * @param {number} offset The positive or negative number of
1570 * tab presses to process.
1571 * @param {[string]} wildmode A 'wildmode' value to
1572 * substitute for the value of the 'wildmode' option.
1575 tab: function tab(offset, wildmode) {
1576 this.autocompleteTimer.flush();
1577 this.ignoredCount = 0;
1579 if (this._caret != this.caret)
1581 this._caret = this.caret;
1583 // Check if we need to run the completer.
1584 if (this.context.waitingForTab || this.wildIndex == -1)
1585 this.complete(true, true);
1587 this.wildtypes = wildmode || options["wildmode"];
1588 let count = Math.abs(offset);
1589 let steps = Math.constrain(this.wildtypes.length - this.wildIndex,
1591 count = Math.max(1, count - steps);
1594 this.wildIndex = Math.min(this.wildIndex, this.wildtypes.length - 1);
1595 switch (this.wildtype.replace(/.*:/, "")) {
1597 this.select(this.nextItem(null));
1600 if (this.itemList.itemCount > 1) {
1601 if (this.substring && this.substring.length > this.completion.length)
1602 this.setCompletion(this.start, this.substring);
1607 let c = steps ? 1 : count;
1608 this.select(offset < 0 ? this.UP : this.DOWN, c, true);
1612 if (this.haveType("list"))
1613 this.itemList.visible = true;
1618 if (this.items.length == 0 && !this.waiting)
1624 * Evaluate a JavaScript expression and return a string suitable
1627 * @param {string} arg
1628 * @param {boolean} useColor When true, the result is a
1629 * highlighted XML object.
1631 echoArgumentToString: function (arg, useColor) {
1635 arg = dactyl.userEval(arg);
1637 arg = util.objectToString(arg, useColor);
1638 else if (callable(arg))
1639 arg = String.replace(arg, "/* use strict */ \n", "/* use strict */ ");
1640 else if (!isString(arg) && useColor)
1641 arg = template.highlight(arg);
1645 commands: function initCommands() {
1649 description: "Echo the expression",
1654 description: "Echo the expression as an error message",
1655 action: dactyl.echoerr
1659 description: "Echo the expression as an informational message",
1660 action: dactyl.echomsg
1662 ].forEach(function (command) {
1663 commands.add([command.name],
1664 command.description,
1666 command.action(CommandLine.echoArgumentToString(args[0] || "", true));
1668 completer: function (context) completion.javascript(context),
1673 commands.add(["mes[sages]"],
1674 "Display previously shown messages",
1676 // TODO: are all messages single line? Some display an aggregation
1677 // of single line messages at least. E.g. :source
1678 if (commandline._messageHistory.length == 1) {
1679 let message = commandline._messageHistory.messages[0];
1680 commandline.echo(message.message, message.highlight, commandline.FORCE_SINGLELINE);
1682 else if (commandline._messageHistory.length > 1) {
1683 commandline.commandOutput(
1684 template.map(commandline._messageHistory.messages, message =>
1685 ["div", { highlight: message.highlight + " Message" },
1691 commands.add(["messc[lear]"],
1692 "Clear the message history",
1693 function () { commandline._messageHistory.clear(); },
1696 commands.add(["sil[ent]"],
1697 "Run a command silently",
1699 commandline.runSilently(() => { commands.execute(args[0] || "", null, true); });
1701 completer: function (context) completion.ex(context),
1706 modes: function initModes() {
1707 initModes.require("editor");
1709 modes.addMode("COMMAND_LINE", {
1711 description: "Active when the command line is focused",
1714 get mappingSelf() commandline.commandSession
1716 // this._extended modes, can include multiple modes, and even main modes
1717 modes.addMode("EX", {
1718 description: "Ex command mode, active when the command line is open for Ex commands",
1719 bases: [modes.COMMAND_LINE]
1721 modes.addMode("PROMPT", {
1722 description: "Active when a prompt is open in the command line",
1723 bases: [modes.COMMAND_LINE]
1726 modes.addMode("INPUT_MULTILINE", {
1727 description: "Active when the command line's multiline input buffer is open",
1728 bases: [modes.INSERT]
1731 mappings: function initMappings() {
1733 mappings.add([modes.COMMAND],
1734 [":"], "Enter Command Line mode",
1735 function () { CommandExMode().open(""); });
1737 mappings.add([modes.INPUT_MULTILINE],
1738 ["<Return>", "<C-j>", "<C-m>"], "Begin a new line",
1739 function ({ self }) {
1740 let text = "\n" + commandline.widgets.multilineInput
1741 .value.substr(0, commandline.widgets.multilineInput.selectionStart)
1744 let index = text.indexOf(self.end);
1747 text = text.substring(1, index);
1750 return () => self.callback.call(commandline, text);
1755 let bind = function bind(...args) mappings.add.apply(mappings, [[modes.COMMAND_LINE]].concat(args));
1757 bind(["<Esc>", "<C-[>"], "Stop waiting for completions or exit Command Line mode",
1758 function ({ self }) {
1759 if (self.completions && self.completions.waiting)
1760 self.completions.waiting = null;
1765 // Any "non-keyword" character triggers abbreviation expansion
1766 // TODO: Add "<CR>" and "<Tab>" to this list
1767 // At the moment, adding "<Tab>" breaks tab completion. Adding
1768 // "<CR>" has no effect.
1769 // TODO: Make non-keyword recognition smarter so that there need not
1770 // be two lists of the same characters (one here and a regexp in
1772 bind(["<Space>", '"', "'"], "Expand command line abbreviation",
1773 function ({ self }) {
1774 self.resetCompletions();
1775 editor.expandAbbreviation(modes.COMMAND_LINE);
1779 bind(["<Return>", "<C-j>", "<C-m>"], "Accept the current input",
1780 function ({ self }) {
1781 if (self.completions)
1782 self.completions.tabTimer.flush();
1784 let command = commandline.command;
1786 self.accepted = true;
1787 return function () { modes.pop(); };
1791 [["<Up>", "<A-p>", "<cmd-prev-match>"], "previous matching", true, true],
1792 [["<S-Up>", "<C-p>", "<cmd-prev>"], "previous", true, false],
1793 [["<Down>", "<A-n>", "<cmd-next-match>"], "next matching", false, true],
1794 [["<S-Down>", "<C-n>", "<cmd-next>"], "next", false, false]
1795 ].forEach(function ([keys, desc, up, search]) {
1796 bind(keys, "Recall the " + desc + " command line from the history list",
1797 function ({ self }) {
1798 dactyl.assert(self.history);
1799 self.history.select(up, search);
1803 bind(["<A-Tab>", "<Tab>", "<A-compl-next>", "<compl-next>"],
1804 "Select the next matching completion item",
1805 function ({ keypressEvents, self }) {
1806 dactyl.assert(self.completions);
1807 self.completions.onTab(keypressEvents[0]);
1810 bind(["<A-S-Tab>", "<S-Tab>", "<A-compl-prev>", "<compl-prev>"],
1811 "Select the previous matching completion item",
1812 function ({ keypressEvents, self }) {
1813 dactyl.assert(self.completions);
1814 self.completions.onTab(keypressEvents[0]);
1817 bind(["<C-Tab>", "<A-f>", "<compl-next-group>"],
1818 "Select the next matching completion group",
1819 function ({ keypressEvents, self }) {
1820 dactyl.assert(self.completions);
1821 self.completions.tabTimer.flush();
1822 self.completions.select(self.completions.CTXT_DOWN);
1825 bind(["<C-S-Tab>", "<A-S-f>", "<compl-prev-group>"],
1826 "Select the previous matching completion group",
1827 function ({ keypressEvents, self }) {
1828 dactyl.assert(self.completions);
1829 self.completions.tabTimer.flush();
1830 self.completions.select(self.completions.CTXT_UP);
1833 bind(["<C-f>", "<PageDown>", "<compl-next-page>"],
1834 "Select the next page of completions",
1835 function ({ keypressEvents, self }) {
1836 dactyl.assert(self.completions);
1837 self.completions.tabTimer.flush();
1838 self.completions.select(self.completions.PAGE_DOWN);
1841 bind(["<C-b>", "<PageUp>", "<compl-prev-page>"],
1842 "Select the previous page of completions",
1843 function ({ keypressEvents, self }) {
1844 dactyl.assert(self.completions);
1845 self.completions.tabTimer.flush();
1846 self.completions.select(self.completions.PAGE_UP);
1849 bind(["<BS>", "<C-h>"], "Delete the previous character",
1851 if (!commandline.command)
1857 bind(["<C-]>", "<C-5>"], "Expand command line abbreviation",
1858 function () { editor.expandAbbreviation(modes.COMMAND_LINE); });
1860 options: function initOptions() {
1861 options.add(["history", "hi"],
1862 "Number of Ex commands and search patterns to store in the command-line history",
1864 { validator: function (value) value >= 0 });
1866 options.add(["maxitems"],
1867 "Maximum number of completion items to display at once",
1869 { validator: function (value) value >= 1 });
1871 options.add(["messages", "msgs"],
1872 "Number of messages to store in the :messages history",
1874 { validator: function (value) value >= 0 });
1876 sanitizer: function initSanitizer() {
1877 sanitizer.addItem("commandline", {
1878 description: "Command-line and search history",
1880 action: function (timespan, host) {
1881 let store = commandline._store;
1882 for (let [k, v] in store) {
1884 store.set(k, v.filter(item =>
1885 !(timespan.contains(item.timestamp) && (!host || commands.hasDomain(item.value, host)))));
1887 store.set(k, v.filter(item => !timespan.contains(item.timestamp)));
1891 // Delete history-like items from the commandline and messages on history purge
1892 sanitizer.addItem("history", {
1893 action: function (timespan, host) {
1894 commandline._store.set("command",
1895 commandline._store.get("command", []).filter(item =>
1896 !(timespan.contains(item.timestamp) && (host ? commands.hasDomain(item.value, host)
1897 : item.privateData))));
1899 commandline._messageHistory.filter(item =>
1900 ( !timespan.contains(item.timestamp * 1000)
1901 || !item.domains && !item.privateData
1902 || host && ( !item.domains
1903 || !item.domains.some(d => util.isSubdomain(d, host)))));
1906 sanitizer.addItem("messages", {
1907 description: "Saved :messages",
1908 action: function (timespan, host) {
1909 commandline._messageHistory.filter(item =>
1910 ( !timespan.contains(item.timestamp * 1000)
1911 || host && ( !item.domains
1912 || !item.domains.some(d => util.isSubdomain(d, host)))));
1919 * The list which is used for the completion box.
1921 * @param {string} id The id of the iframe which will display the list. It
1922 * must be in its own container element, whose height it will update as
1926 var ItemList = Class("ItemList", {
1929 init: function init(frame) {
1932 this.doc = frame.contentDocument;
1933 this.win = frame.contentWindow;
1934 this.body = this.doc.body;
1935 this.container = frame.parentNode;
1937 highlight.highlightNode(this.doc.body, "Comp");
1939 this._onResize = Timer(20, 400, function _onResize(event) {
1941 this.onResize(event);
1943 this._resize = Timer(20, 400, function _resize(flags) {
1948 DOM(this.win).resize(this._onResize.bound.tell);
1952 ["div", { highlight: "Normal", style: "white-space: nowrap", key: "root" },
1953 ["div", { key: "wrapper" },
1954 ["div", { highlight: "Completions", key: "noCompletions" },
1955 ["span", { highlight: "Title" },
1956 _("completion.noCompletions")]],
1957 ["div", { key: "completions" }]],
1959 ["div", { highlight: "Completions" },
1960 template.map(util.range(0, options["maxitems"] * 2), i =>
1961 ["div", { highlight: "CompItem NonText" },
1964 get itemCount() this.context.contextList
1965 .reduce((acc, ctxt) => acc + ctxt.items.length, 0),
1967 get visible() !this.container.collapsed,
1968 set visible(val) this.container.collapsed = !val,
1970 get activeGroups() this.context.contextList
1971 .filter(c => c.items.length || c.message || c.incomplete)
1972 .map(this.getGroup, this),
1974 get selected() let (g = this.selectedGroup) g && g.selectedIdx != null
1975 ? [g.context, g.selectedIdx] : null,
1977 getRelativeItem: function getRelativeItem(offset, tuple, noWrap) {
1978 let groups = this.activeGroups;
1982 let group = this.selectedGroup || groups[0];
1983 let start = group.selectedIdx || 0;
1984 if (tuple === null) { // Kludge.
1986 tuple = [this.activeGroups[0], -1];
1988 let group = this.activeGroups.slice(-1)[0];
1989 tuple = [group, group.itemCount];
1993 [group, start] = tuple;
1995 group = this.getGroup(group);
1997 start = (group.offsets.start + start + offset);
1999 start %= this.itemCount || 1;
2000 if (start < 0 && (!noWrap || arguments[1] === null))
2001 start += this.itemCount;
2003 if (noWrap && offset > 0) {
2004 // Check if we've passed any incomplete contexts
2006 let i = groups.indexOf(group);
2007 util.assert(i >= 0, undefined, false);
2008 for (; i < groups.length; i++) {
2009 let end = groups[i].offsets.start + groups[i].itemCount;
2010 if (start >= end && groups[i].context.incomplete)
2011 return [groups[i].context, start - groups[i].offsets.start];
2018 if (start < 0 || start >= this.itemCount)
2021 group = groups.find(g => let (i = start - g.offsets.start) i >= 0 && i < g.itemCount);
2022 return [group.context, start - group.offsets.start];
2025 getRelativePage: function getRelativePage(offset, tuple, noWrap) {
2026 offset *= this.maxItems;
2027 // Try once with wrapping disabled.
2028 let res = this.getRelativeItem(offset, tuple, true);
2032 let sign = offset / Math.abs(offset);
2034 let off = this.getOffset(tuple === null ? null : tuple || this.selected);
2036 // Unselected. Defer to getRelativeItem.
2037 res = this.getRelativeItem(offset, null, noWrap);
2038 else if (~[0, this.itemCount - 1].indexOf(off))
2039 // At start or end. Jump to other end.
2040 res = this.getRelativeItem(sign, null, noWrap);
2042 // Wrapped. Go to beginning or end.
2043 res = this.getRelativeItem(-sign, null);
2049 * Initializes the ItemList for use with a new root completion
2052 * @param {CompletionContext} context The new root context.
2054 open: function open(context) {
2055 this.context = context;
2057 this.container.height = 0;
2059 this.maxItems = options["maxitems"];
2061 DOM(this.rootXML, this.doc, this.nodes)
2062 .appendTo(DOM(this.body).empty());
2068 * Updates the absolute result indices of all groups after
2069 * results have changed.
2072 updateOffsets: function updateOffsets() {
2073 let total = this.itemCount;
2075 for (let group in values(this.activeGroups)) {
2076 group.offsets = { start: count, end: total - count - group.itemCount };
2077 count += group.itemCount;
2082 * Updates the set and state of active groups for a new set of
2083 * completion results.
2085 update: function update() {
2086 DOM(this.nodes.completions).empty();
2088 let container = DOM(this.nodes.completions);
2089 let groups = this.activeGroups;
2090 for (let group in values(groups)) {
2092 container.append(group.nodes.root);
2095 this.updateOffsets();
2097 DOM(this.nodes.noCompletions).toggle(!groups.length);
2099 this.startPos = null;
2100 this.select(groups[0] && groups[0].context, null);
2102 this._resize.tell();
2106 * Updates the group for *context* after an asynchronous update
2109 * @param {CompletionContext} context The context which has
2112 updateContext: function updateContext(context) {
2113 let group = this.getGroup(context);
2114 this.updateOffsets();
2116 if (~this.activeGroups.indexOf(group))
2119 DOM(group.nodes.root).remove();
2120 if (this.selectedGroup == group)
2121 this.selectedGroup = null;
2124 let g = this.selectedGroup;
2125 this.select(g, g && g.selectedIdx);
2129 * Updates the DOM to reflect the current state of all groups.
2132 draw: function draw() {
2133 for (let group in values(this.activeGroups))
2136 // We need to collect all of the rescrolling functions in
2137 // one go, as the height calculation that they need to do
2138 // would force an expensive reflow after each call due to
2139 // DOM modifications, otherwise.
2140 this.activeGroups.filter(g => !g.collapsed)
2141 .map(g => g.rescrollFunc)
2145 this.win.scrollTo(0, 0);
2147 this._resize.tell(ItemList.RESIZE_BRIEF);
2150 onResize: function onResize() {
2151 if (this.selectedGroup)
2152 this.selectedGroup.rescrollFunc();
2158 * Resizes the list after an update.
2161 resize: function resize(flags) {
2162 let { completions, root } = this.nodes;
2165 root.style.minWidth = document.getElementById("dactyl-commandline").scrollWidth + "px";
2167 let { minHeight } = this;
2168 if (mow.visible && this.isAboveMow) // Kludge.
2169 minHeight -= mow.wantedHeight;
2171 let needed = this.win.scrollY + DOM(completions).rect.bottom;
2172 this.minHeight = Math.max(minHeight, needed);
2175 root.style.minWidth = "";
2177 let height = this.visible ? parseFloat(this.container.height) : 0;
2178 if (this.minHeight <= minHeight || !mow.visible)
2179 this.container.height = Math.min(this.minHeight,
2180 height + config.outputHeight - mow.spaceNeeded);
2182 // FIXME: Belongs elsewhere.
2183 mow.resize(false, Math.max(0, this.minHeight - this.container.height));
2185 this.container.height = this.minHeight - mow.spaceNeeded;
2187 this.timeout(function () {
2188 this.container.height -= mow.spaceNeeded;
2194 * Selects the item at the given *group* and *index*.
2196 * @param {CompletionContext|[CompletionContext,number]} *group* The
2197 * completion context to select, or a tuple specifying the
2198 * context and item index.
2199 * @param {number} index The item index in *group* to select.
2200 * @param {number} position If non-null, try to position the
2201 * selected item at the *position*th row from the top of
2202 * the screen. Note that at least {@link #CONTEXT_LINES}
2203 * lines will be visible above and below the selected item
2204 * unless there aren't enough results to make this possible.
2207 select: function select(group, index, position) {
2209 [group, index] = group;
2211 group = this.getGroup(group);
2213 if (this.selectedGroup && (!group || group != this.selectedGroup))
2214 this.selectedGroup.selectedIdx = null;
2216 this.selectedGroup = group;
2219 group.selectedIdx = index;
2221 let groups = this.activeGroups;
2223 if (position != null || !this.startPos && groups.length)
2224 this.startPos = [group || groups[0], position || 0];
2226 if (groups.length) {
2227 group = group || groups[0];
2228 let idx = groups.indexOf(group);
2230 let start = this.startPos[0].getOffset(this.startPos[1]);
2232 let idx = group.selectedIdx || 0;
2233 let off = group.getOffset(idx);
2235 start = Math.constrain(start,
2236 off + Math.min(this.CONTEXT_LINES,
2237 group.itemCount - idx + group.offsets.end)
2238 - this.maxItems + 1,
2239 off - Math.min(this.CONTEXT_LINES,
2240 idx + group.offsets.start));
2243 let count = this.maxItems;
2244 for (let group in values(groups)) {
2245 let off = Math.max(0, start - group.offsets.start);
2247 group.count = Math.constrain(group.itemCount - off, 0, count);
2248 count -= group.count;
2250 group.collapsed = group.offsets.start >= start + this.maxItems
2251 || group.offsets.start + group.itemCount < start;
2253 group.range = ItemList.Range(off, off + group.count);
2256 var startPos = [group, group.range.start];
2258 this.startPos = startPos;
2264 * Returns an ItemList group for the given completion context,
2265 * creating one if necessary.
2267 * @param {CompletionContext} context
2268 * @returns {ItemList.Group}
2270 getGroup: function getGroup(context)
2271 context instanceof ItemList.Group ? context
2272 : context && context.getCache("itemlist-group",
2273 () => ItemList.Group(this, context)),
2275 getOffset: function getOffset(tuple) tuple && this.getGroup(tuple[0]).getOffset(tuple[1])
2277 RESIZE_BRIEF: 1 << 0,
2279 WAITING_MESSAGE: _("completion.generating"),
2281 Group: Class("ItemList.Group", {
2282 init: function init(parent, context) {
2283 this.parent = parent;
2284 this.context = context;
2286 this.range = ItemList.Range(0, 0);
2290 ["div", { key: "root", highlight: "CompGroup" },
2291 ["div", { highlight: "Completions" },
2292 this.context.createRow(this.context.title || [], "CompTitle")],
2293 ["div", { highlight: "CompTitleSep" }],
2294 ["div", { key: "contents" },
2295 ["div", { key: "up", highlight: "CompLess" }],
2296 ["div", { key: "message", highlight: "CompMsg" },
2297 this.context.message || []],
2298 ["div", { key: "itemsContainer", class: "completion-items-container" },
2299 ["div", { key: "items", highlight: "Completions" }]],
2300 ["div", { key: "waiting", highlight: "CompMsg" },
2301 ItemList.WAITING_MESSAGE],
2302 ["div", { key: "down", highlight: "CompMore" }]]],
2304 get doc() this.parent.doc,
2305 get win() this.parent.win,
2306 get maxItems() this.parent.maxItems,
2308 get itemCount() this.context.items.length,
2311 * Returns a function which will update the scroll offsets
2312 * and heights of various DOM members.
2315 get rescrollFunc() {
2316 let container = this.nodes.itemsContainer;
2317 let pos = DOM(container).rect.top;
2318 let start = DOM(this.getRow(this.range.start)).rect.top;
2319 let height = DOM(this.getRow(this.range.end - 1)).rect.bottom - start || 0;
2320 let scroll = start + container.scrollTop - pos;
2323 let row = this.selectedRow;
2324 if (row && this.parent.minHeight) {
2325 let { rect } = DOM(this.selectedRow);
2326 var scrollY = this.win.scrollY + rect.bottom - this.win.innerHeight;
2329 return function () {
2330 container.style.height = height + "px";
2331 container.scrollTop = scroll;
2332 if (scrollY != null)
2333 win.scrollTo(0, Math.max(scrollY, 0));
2338 * Reset this group for use with a new set of results.
2340 reset: function reset() {
2342 this.generatedRange = ItemList.Range(0, 0);
2344 DOM.fromJSON(this.rootXML, this.doc, this.nodes);
2348 * Update this group after an asynchronous results push.
2350 update: function update() {
2351 this.generatedRange = ItemList.Range(0, 0);
2352 DOM(this.nodes.items).empty();
2354 if (this.context.message)
2355 DOM(this.nodes.message).empty()
2356 .append(DOM.fromJSON(this.context.message, this.doc));
2358 if (this.selectedIdx > this.itemCount)
2359 this.selectedIdx = null;
2363 * Updates the DOM to reflect the current state of this
2367 draw: function draw() {
2368 DOM(this.nodes.contents).toggle(!this.collapsed);
2372 DOM(this.nodes.message).toggle(this.context.message && this.range.start == 0);
2373 DOM(this.nodes.waiting).toggle(this.context.incomplete && this.range.end <= this.itemCount);
2374 DOM(this.nodes.up).toggle(this.range.start > 0);
2375 DOM(this.nodes.down).toggle(this.range.end < this.itemCount);
2377 if (!this.generatedRange.contains(this.range)) {
2378 if (this.generatedRange.end == 0)
2379 var [start, end] = this.range;
2381 start = this.range.start - (this.range.start <= this.generatedRange.start
2382 ? this.maxItems / 2 : 0);
2383 end = this.range.end + (this.range.end > this.generatedRange.end
2384 ? this.maxItems / 2 : 0);
2387 let range = ItemList.Range(Math.max(0, start - start % 2),
2388 Math.min(this.itemCount, end));
2391 for (let [i, row] in this.context.getRows(this.generatedRange.start,
2392 this.generatedRange.end,
2394 if (!range.contains(i))
2399 let container = DOM(this.nodes.items);
2400 let before = first ? DOM(first).bound.before
2401 : DOM(this.nodes.items).bound.append;
2403 for (let [i, row] in this.context.getRows(range.start, range.end,
2405 if (i < this.generatedRange.start)
2407 else if (i >= this.generatedRange.end)
2408 container.append(row);
2409 if (i == this.selectedIdx)
2410 this.selectedIdx = this.selectedIdx;
2413 this.generatedRange = range;
2417 getRow: function getRow(idx) this.context.getRow(idx, this.doc),
2419 getOffset: function getOffset(idx) this.offsets.start + (idx || 0),
2421 get selectedRow() this.getRow(this._selectedIdx),
2423 get selectedIdx() this._selectedIdx,
2424 set selectedIdx(idx) {
2425 if (this.selectedRow && this._selectedIdx != idx)
2426 DOM(this.selectedRow).attr("selected", null);
2428 this._selectedIdx = idx;
2430 if (this.selectedRow)
2431 DOM(this.selectedRow).attr("selected", true);
2435 Range: Class.Memoize(function () {
2436 let Range = Struct("ItemList.Range", "start", "end");
2437 update(Range.prototype, {
2438 contains: function contains(idx)
2439 typeof idx == "number" ? idx >= this.start && idx < this.end
2440 : this.contains(idx.start) &&
2441 idx.end >= this.start && idx.end <= this.end
2447 // vim: set fdm=marker sw=4 sts=4 ts=8 et: