From 287dfb424e8ffdf81baf70dc2e57bb09c678b6c9 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Wed, 3 Dec 2008 21:06:26 -0500 Subject: [PATCH] Allow completion of ;hint modes --- liberator/content/hints.js | 14 ++++++-- liberator/content/liberator.js | 8 ++--- liberator/content/modes.js | 5 +++ liberator/content/options.js | 4 +-- liberator/content/ui.js | 58 ++++++++++++++++++++-------------- 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/liberator/content/hints.js b/liberator/content/hints.js index c1f18eb1..d231ffd4 100644 --- a/liberator/content/hints.js +++ b/liberator/content/hints.js @@ -598,8 +598,18 @@ function Hints() //{{{ mappings.add(myModes, [";"], "Start an extended hint mode", - function (arg) { hints.show(arg); }, - { flags: Mappings.flags.ARGUMENT }); + function (arg) { + commandline.input(";", function (arg) { + setTimeout(function () { hints.show(arg) }, 0); + }, + { + completer: function (context) { + context.completions = [[k, v.prompt] for ([k, v] in Iterator(hintModes))] + }, + onChange: function () { commandline.close() } + }); + }); + //{ flags: Mappings.flags.ARGUMENT }); /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// diff --git a/liberator/content/liberator.js b/liberator/content/liberator.js index 1260e44e..4046e85a 100644 --- a/liberator/content/liberator.js +++ b/liberator/content/liberator.js @@ -600,14 +600,14 @@ const liberator = (function () //{{{ callbacks.push([type, mode, func]); }, - triggerCallback: function (type, mode, data) + triggerCallback: function (type, mode) { // liberator.dump("type: " + type + " mode: " + mode + "data: " + data + "\n"); - for (let i = 0; i < callbacks.length; i++) + for (let [,callback] in Iterator(callbacks)) { - var [thistype, thismode, thisfunc] = callbacks[i]; + var [thistype, thismode, thisfunc] = callbacks; if (mode == thismode && type == thistype) - return thisfunc.call(this, data); + return thisfunc.apply(this, Array.slice(arguments, 2)); } return false; }, diff --git a/liberator/content/modes.js b/liberator/content/modes.js index b8ab553a..deebb6b5 100644 --- a/liberator/content/modes.js +++ b/liberator/content/modes.js @@ -152,6 +152,11 @@ const modes = (function () //{{{ mainModes.push(this[name]); }, + getMode: function (name) + { + return modeMap[name]; + }, + // show the current mode string in the command line show: function () { diff --git a/liberator/content/options.js b/liberator/content/options.js index e376c16e..b0469408 100644 --- a/liberator/content/options.js +++ b/liberator/content/options.js @@ -45,7 +45,7 @@ function Option(names, description, type, defaultValue, extraInfo) //{{{ this.description = description || ""; // "", 0 are valid default values - this.defaultValue = (defaultValue === undefined) ? null : defaultValue; + this.defaultValue = (defaultValue == null) ? null : defaultValue; this.setter = extraInfo.setter || null; this.getter = extraInfo.getter || null; @@ -73,7 +73,7 @@ function Option(names, description, type, defaultValue, extraInfo) //{{{ } Option.prototype = { get globalvalue() options.store.get(this.name), - set globalvalue(val) { options.store.set(this.name, val) }, + set globalvalue(val) options.store.set(this.name, val), parseValues: function (value) { diff --git a/liberator/content/ui.js b/liberator/content/ui.js index 5a3dda32..4ae2ca00 100644 --- a/liberator/content/ui.js +++ b/liberator/content/ui.js @@ -356,11 +356,6 @@ function CommandLine() //{{{ ////////////////////// CALLBACKS /////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ - // callback for prompt mode - var promptSubmitCallback = null; - var promptChangeCallback = null; - var promptCompleter = null; - liberator.registerCallback("submit", modes.EX, function (command) { liberator.execute(command); }); liberator.registerCallback("complete", modes.EX, function (context) { context.fork("ex", 0, completion, "ex"); @@ -374,15 +369,20 @@ function CommandLine() //{{{ liberator.registerCallback("cancel", modes.PROMPT, closePrompt); liberator.registerCallback("submit", modes.PROMPT, closePrompt); - liberator.registerCallback("change", modes.PROMPT, function (str) { + liberator.registerCallback("change", modes.PROMPT, function (str, ok) { + liberator.triggerCallback("change", modes.EX, str); if (promptChangeCallback) - return promptChangeCallback(str); + promptChangeCallback(str, ok); }); liberator.registerCallback("complete", modes.PROMPT, function (context) { if (promptCompleter) - promptCompleter(context); + context.fork("prompt", 0, commandline, promptCompleter); }); + var promptSubmitCallback = null; + var promptChangeCallback = null; + var promptCompleter = null; + function closePrompt(value) { let callback = promptSubmitCallback; @@ -390,7 +390,7 @@ function CommandLine() //{{{ currentExtendedMode = null; commandline.clear(); if (callback) - callback(value); + callback.call(commandline, value); } /////////////////////////////////////////////////////////////////////////////}}} @@ -556,6 +556,22 @@ function CommandLine() //{{{ return arg; } + function close(callback) + { + let command = commandline.getCommand(); + inputHistory.add(command); + commandline.resetCompletions(); + completionList.hide(); + statusline.updateProgress(""); // we may have a "match x of y" visible + + let mode = currentExtendedMode; // save it here, as setMode() resets it + currentExtendedMode = null; /* Don't let modes.pop trigger "cancel" */ + modes.pop(!commandline.silent); + + liberator.focusContent(false); + return liberator.triggerCallback(callback, mode, command, callback == "submit"); + } + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// OPTIONS ///////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ @@ -842,11 +858,9 @@ function CommandLine() //{{{ }, // normally used when pressing esc, does not execute a command - close: function close() + close: function () { - let res = liberator.triggerCallback("cancel", currentExtendedMode); - inputHistory.add(this.getCommand()); - statusline.updateProgress(""); // we may have a "match x of y" visible + close("cancel"); this.clear(); }, @@ -910,11 +924,14 @@ function CommandLine() //{{{ promptSubmitCallback = callback; promptChangeCallback = extra.onChange; promptCompleter = extra.completer; + modes.push(modes.COMMAND_LINE, modes.PROMPT); currentExtendedMode = modes.PROMPT; setPrompt(prompt + " ", this.HL_QUESTION); setCommand(extra.default || ""); commandWidget.focus(); + + completions = new Completions(CompletionContext(commandWidget.inputField.editor)); }, // reads a multi line input and returns the string once the last line matches @@ -937,7 +954,8 @@ function CommandLine() //{{{ onEvent: function onEvent(event) { - completions.previewClear(); + if (completions) + completions.previewClear(); let command = this.getCommand(); if (event.type == "blur") @@ -955,7 +973,7 @@ function CommandLine() //{{{ } else if (event.type == "focus") { - if (!currentExtendedMode && event.target == commandWidget.inputField) + if (liberator.mode != modes.COMMAND_LINE && event.target == commandWidget.inputField) { event.target.blur(); liberator.beep(); @@ -978,15 +996,7 @@ function CommandLine() //{{{ // FIXME: should trigger "cancel" event if (events.isAcceptKey(key)) { - let mode = currentExtendedMode; // save it here, as setMode() resets it - currentExtendedMode = null; /* Don't let modes.pop trigger "cancel" */ - inputHistory.add(command); - modes.pop(!commandline.silent); - this.resetCompletions(); - completionList.hide(); - liberator.focusContent(false); - statusline.updateProgress(""); // we may have a "match x of y" visible - return liberator.triggerCallback("submit", mode, command); + return close("submit"); } // user pressed UP or DOWN arrow to cycle history completion else if (/^(|||||)$/.test(key))