1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 11:58:00 +01:00

Add template.listCompleter to list completion results to the MOW.

This commit is contained in:
Kris Maglione
2008-11-25 07:30:23 +00:00
parent b543359408
commit 3affc98ea5
10 changed files with 105 additions and 136 deletions

View File

@@ -301,9 +301,7 @@ function Bookmarks() //{{{
if (bookmarks.add(false, title, url, keyword, tags, special)) if (bookmarks.add(false, title, url, keyword, tags, special))
{ {
var extra = ""; let extra = (title == url) ? "" : " (" + title + ")";
if (title != url)
extra = " (" + title + ")";
liberator.echo("Added bookmark: " + url + extra, commandline.FORCE_SINGLELINE); liberator.echo("Added bookmark: " + url + extra, commandline.FORCE_SINGLELINE);
} }
else else
@@ -561,9 +559,13 @@ function Bookmarks() //{{{
// if openItems is true, open the matching bookmarks items in tabs rather than display // if openItems is true, open the matching bookmarks items in tabs rather than display
list: function (filter, tags, openItems) list: function (filter, tags, openItems)
{ {
var items = this.get(filter, tags, false); if (!openItems)
if (items.length == 0) return template.listCompleter("bookmark", filter, tags);
{
let items = this.get(filter, tags, false);
if (items.length)
return liberator.open(items.map(function (i) i.url), liberator.NEW_TAB);
if (filter.length > 0 && tags.length > 0) if (filter.length > 0 && tags.length > 0)
liberator.echoerr("E283: No bookmarks matching tags: \"" + tags + "\" and string: \"" + filter + "\""); liberator.echoerr("E283: No bookmarks matching tags: \"" + tags + "\" and string: \"" + filter + "\"");
else if (filter.length > 0) else if (filter.length > 0)
@@ -572,15 +574,6 @@ function Bookmarks() //{{{
liberator.echoerr("E283: No bookmarks matching tags: \"" + tags + "\""); liberator.echoerr("E283: No bookmarks matching tags: \"" + tags + "\"");
else else
liberator.echoerr("No bookmarks set"); liberator.echoerr("No bookmarks set");
return;
}
if (openItems)
return liberator.open([i.url for each (i in items)], liberator.NEW_TAB);
let list = template.genericTable(items, this.format);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
} }
}; };
//}}} //}}}
@@ -808,22 +801,17 @@ function History() //{{{
// if openItems is true, open the matching history items in tabs rather than display // if openItems is true, open the matching history items in tabs rather than display
list: function (filter, openItems) list: function (filter, openItems)
{ {
var items = this.get(filter, 1000); if (!openItems)
if (items.length == 0) return template.listCompleter("history", filter);
{
var items = this.get({ searchTerms: filter }, 1000);
if (items.length)
return liberator.open([i[0] for each (i in items)], liberator.NEW_TAB);
if (filter.length > 0) if (filter.length > 0)
liberator.echoerr("E283: No history matching \"" + filter + "\""); liberator.echoerr("E283: No history matching \"" + filter + "\"");
else else
liberator.echoerr("No history set"); liberator.echoerr("No history set");
return;
}
if (openItems)
return liberator.open([i[0] for each (i in items)], liberator.NEW_TAB);
let list = template.genericTable(items, this.format);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
} }
}; };
//}}} //}}}
@@ -942,6 +930,7 @@ function QuickMarks() //{{{
add: function (qmark, location) add: function (qmark, location)
{ {
qmarks.set(qmark, location); qmarks.set(qmark, location);
liberator.echo("Added Quick Mark '" + qmark + "': " + location);
}, },
remove: function (filter) remove: function (filter)
@@ -997,9 +986,7 @@ function QuickMarks() //{{{
} }
let items = [[mark, qmarks.get(mark)] for ([k, mark] in Iterator(marks))]; let items = [[mark, qmarks.get(mark)] for ([k, mark] in Iterator(marks))];
template.genericTable(items, { title: ["QuickMark", "URL"] });
let list = template.genericTable(items, { title: ["QuickMark", "URL"] });
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
} }
}; };
//}}} //}}}

View File

@@ -818,7 +818,8 @@ function Commands() //{{{
cmd.argCount, cmd.argCount,
cmd.count ? "0c" : "", cmd.count ? "0c" : "",
completerToString(cmd.completer), completerToString(cmd.completer),
cmd.replacementText || "function () { ... }"] for each (cmd in cmds))); cmd.replacementText || "function () { ... }"]
for each (cmd in cmds)));
commandline.echo(str, commandline.HL_NORMAL, commandline.FORCE_MULTILINE); commandline.echo(str, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
} }

View File

@@ -149,7 +149,7 @@ CompletionContext.prototype = {
this.incomplete = true; this.incomplete = true;
liberator.callFunctionInThread(null, function () { liberator.callFunctionInThread(null, function () {
let items = self.generate(); let items = self.generate();
if (self.backgroundLock != lock) if (self.cache.backgroundLock != lock)
return; return;
self.incomplete = false; self.incomplete = false;
self.completions = items; self.completions = items;
@@ -244,12 +244,14 @@ CompletionContext.prototype = {
function (i) cache[i] = cache[i] || util.xmlToDom(self.createRow(items[i]), doc)); function (i) cache[i] = cache[i] || util.xmlToDom(self.createRow(items[i]), doc));
}, },
fork: function fork(name, offset, completer, self) fork: function fork(name, offset, self, completer)
{ {
if (typeof completer == "string")
completer = self[completer]
let context = new CompletionContext(this, name, offset); let context = new CompletionContext(this, name, offset);
this.contextList.push(context); this.contextList.push(context);
if (completer) if (completer)
return completer.apply(self, [context].concat(Array.slice(arguments, 4))); return completer.apply(self || this, [context].concat(Array.slice(arguments, 4)));
return context; return context;
}, },
@@ -683,13 +685,13 @@ function Completion() //{{{
for (let [,obj] in Iterator(objects)) for (let [,obj] in Iterator(objects))
{ {
obj[3] = compl || this.objectKeys(obj); obj[3] = compl || this.objectKeys(obj);
this.context.fork(obj[1], top[OFFSET], this.filter, this, this.context.fork(obj[1], top[OFFSET], this, "filter",
obj[3], obj[1], true, key + (string || ""), last, key.length); obj[3], obj[1], true, key + (string || ""), last, key.length);
} }
for (let [,obj] in Iterator(objects)) for (let [,obj] in Iterator(objects))
{ {
obj[1] += " (substrings)"; obj[1] += " (substrings)";
this.context.fork(obj[1], top[OFFSET], this.filter, this, this.context.fork(obj[1], top[OFFSET], this, "filter",
obj[3], obj[1], false, key + (string || ""), last, key.length); obj[3], obj[1], false, key + (string || ""), last, key.length);
} }
} }
@@ -1063,15 +1065,19 @@ function Completion() //{{{
} }
}, },
buffer: function buffer(filter) buffer: function buffer(context)
{ {
// FIXME: liberator.has("tabs") filter = context.filter.toLowerCase();
let items = []; context.title = ["Buffer", "URL"];
let xml = <table/> context.keys = { text: "text", description: "url", icon: "icon" };
filter = (filter || "").toLowerCase(); let process = context.process[0];
context.process = [function ({ text: text, item: item }) <>
<span style="display: inline-block; text-align: right; width: 3ex">{item.i}</span>
<span class="hl-Indicator" style="display: inline-block; width: 1.5em; text-align: center">{item.indicator}</span>
{ process.call(this, { item: item, text: text }) }
</>];
for (let [i, browser] in tabs.browsers) context.completions = util.map(tabs.browsers, function ([i, browser]) {
{
if (i == tabs.index()) if (i == tabs.index())
indicator = "%" indicator = "%"
else if (i == tabs.index(tabs.alternate)) else if (i == tabs.index(tabs.alternate))
@@ -1079,43 +1085,18 @@ function Completion() //{{{
else else
indicator = " "; indicator = " ";
let tab = tabs.getTab(i);
i = i + 1; i = i + 1;
let title = "";
try
{
title = browser.contentDocument.title;
}
catch (e) {}
let url = browser.contentDocument.location.href; let url = browser.contentDocument.location.href;
if (title.indexOf(filter) != -1 || url.indexOf(filter) != -1 || return {
String.indexOf(i, filter) != -1) text: [i + ": " + (tab.label || "(Untitled)"), i + ": " + url],
{ url: url,
if (title == "") indicator: indicator,
title = "(Untitled)"; i: i,
icon: tab.image
items.push([[i + ": " + title, i + ": " + url], url]); };
});
let icon = "";
if (liberator.has("bookmarks"))
icon = bookmarks.getFavicon(url);
xml.* +=
<ul class="hl-CompItem">
<li align="right"> {i}</li>
<li><span class="hl-Indicator"> {indicator} </span></li>
<li class="hl-CompIcon">{icon ? <img src={icon}/> : <></>}</li>
<li class="hl-CompResult" style="width: 250px; max-width: 500px; overflow: hidden">{title}</li>
<li class="hl-CompDesc"><a href="#" class="hl-URL buffer-list">{url}</a></li>
</ul>;
}
}
if (!filter)
return [0, items.map(function ([a, b]) [a[0], b]), xml];
return [0, buildLongestCommonSubstring(items, filter), xml];
}, },
colorScheme: function colorScheme(filter) colorScheme: function colorScheme(filter)
@@ -1174,7 +1155,7 @@ function Completion() //{{{
let [, prefix, junk] = context.filter.match(/^(:*\d*)\w*(.?)/) || []; let [, prefix, junk] = context.filter.match(/^(:*\d*)\w*(.?)/) || [];
context.advance(prefix.length) context.advance(prefix.length)
if (!junk) if (!junk)
return context.fork("", 0, this.command); return context.fork("", 0, this, "command");
// dynamically get completions as specified with the command's completer function // dynamically get completions as specified with the command's completer function
let command = commands.get(cmd); let command = commands.get(cmd);
@@ -1239,7 +1220,7 @@ function Completion() //{{{
function (file) [(tail ? file.leafName : dir + file.leafName).replace(" ", "\\ ", "g"), function (file) [(tail ? file.leafName : dir + file.leafName).replace(" ", "\\ ", "g"),
file.isDirectory() ? "Directory" : "File", file.isDirectory() ? "Directory" : "File",
file.isDirectory() ? "resource://gre/res/html/folder.png" file.isDirectory() ? "resource://gre/res/html/folder.png"
: "moz-icon://" + makeFileURI(file).path] : "moz-icon://" + file.leafName]
); );
} }
catch (e) {} catch (e) {}
@@ -1337,12 +1318,12 @@ function Completion() //{{{
if (!space) if (!space)
return; return;
context.fork("suggest", keyword.length + space.length, this.searchEngineSuggest, this, context.fork("suggest", keyword.length + space.length, this, "searchEngineSuggest",
keyword, true); keyword, true);
let item = keywords.filter(function (k) k.keyword == keyword)[0]; let item = keywords.filter(function (k) k.keyword == keyword)[0];
if (item && item.url.indexOf("%s") > -1) if (item && item.url.indexOf("%s") > -1)
context.fork("keyword/" + keyword, keyword.length + space.length, function (context) { context.fork("keyword/" + keyword, keyword.length + space.length, null, function (context) {
context.format = history.format; context.format = history.format;
context.title = [keyword + " Quick Search"]; context.title = [keyword + " Quick Search"];
context.background = true; context.background = true;
@@ -1458,7 +1439,7 @@ function Completion() //{{{
// Will, and should, throw an error if !(c in opts) // Will, and should, throw an error if !(c in opts)
Array.forEach(complete || options["complete"], Array.forEach(complete || options["complete"],
function (c) context.fork(c, 0, completion.urlCompleters[c].completer, completion)); function (c) context.fork(c, 0, completion, completion.urlCompleters[c].completer));
}, },
urlCompleters: {}, urlCompleters: {},

View File

@@ -928,22 +928,12 @@ function Editor() //{{{
var searchFilter = (filter == "!") ? "!ci" var searchFilter = (filter == "!") ? "!ci"
: filter + "!"; // ! -> list all, on c or i ! matches too) : filter + "!"; // ! -> list all, on c or i ! matches too)
let list = let list = [[rhs[0], lhs, rhs[1]] for ([lhs, rhs] in abbrevs()) if (searchFilter.indexOf(rhs[0]) > -1)];
<table>
{ if (!list.length)
template.map(abbrevs(), function ([lhs, rhs]) return liberator.echoerr("No abbreviations found");
searchFilter.indexOf(rhs[0]) < 0 ? undefined : list = template.tabular(["", "LHS", "RHS"], [], list);
<tr>
<td>{rhs[0]}</td>
<td>{lhs}</td>
<td>{rhs[1]}</td>
</tr>)
}
</table>;
if (list.*.length())
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE); commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
else
liberator.echoerr("No abbreviations found");
} }
}, },

View File

@@ -1249,12 +1249,12 @@ const liberator = (function () //{{{
autocommands.trigger(config.name + "Leave", {}); autocommands.trigger(config.name + "Leave", {});
}, },
sleep: function (ms) sleep: function (delay)
{ {
var mainThread = threadManager.mainThread; let mainThread = threadManager.mainThread;
var then = new Date().getTime(), now = then; let end = Date.now() + delay;
for (; now - then < ms; now = new Date().getTime()) while (Date.now() < end)
mainThread.processNextEvent(true); mainThread.processNextEvent(true);
return true; return true;
}, },
@@ -1268,11 +1268,16 @@ const liberator = (function () //{{{
callback(); callback();
}, },
threadYield: function (flush) threadYield: function (flush, interruptable)
{ {
let mainThread = threadManager.mainThread; let mainThread = threadManager.mainThread;
liberator.interrupted = false;
do do
{
mainThread.processNextEvent(true); mainThread.processNextEvent(true);
if (liberator.interrupted)
throw new Error("Interrupted");
}
while (flush && mainThread.hasPendingEvents()); while (flush && mainThread.hasPendingEvents());
}, },

View File

@@ -965,13 +965,11 @@ function Options() //{{{
continue; continue;
value = options.getPref(pref); value = options.getPref(pref);
if (typeof value == "string")
value = value.substr(0, 100).replace(/\n/g, " ");
let option = { let option = {
isDefault: !userValue, isDefault: !userValue,
default: loadPreference(pref, null, true), default: loadPreference(pref, null, true),
value: <>={template.highlight(value)}</>, value: <>={template.highlight(value, true, 100)}</>,
name: pref, name: pref,
pre: "\u00a0\u00a0" /* Unicode nonbreaking space. */ pre: "\u00a0\u00a0" /* Unicode nonbreaking space. */
}; };

View File

@@ -354,7 +354,7 @@ function Tabs() //{{{
{ {
bang: true, bang: true,
count: true, count: true,
completer: function (context) completion.buffer(context.filter), completer: function (context) completion.buffer(context),
literal: true literal: true
}); });
@@ -480,7 +480,7 @@ function Tabs() //{{{
{ {
bang: true, bang: true,
count: true, count: true,
completer: function (context) completion.buffer(context.filter), completer: function (context) completion.buffer(context),
literal: true literal: true
}); });
@@ -748,8 +748,7 @@ function Tabs() //{{{
list: function (filter) list: function (filter)
{ {
let list = template.generic(completion.buffer(filter)[2]); template.listCompleter("buffer", filter);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
}, },
// wrap causes the movement to wrap around the start and end of the tab list // wrap causes the movement to wrap around the start and end of the tab list

View File

@@ -32,6 +32,23 @@ const template = {
return <>{xml}</>; return <>{xml}</>;
}, },
listCompleter: function (name, filter)
{
let context = new CompletionContext(filter || "");
context.fork.apply(context, ["list", 0, completion, name].concat(Array.slice(arguments, 2)));
context = context.contexts["/list"];
while (context.incomplete)
liberator.threadYield(true, true);
let list = this.generic(
<div class="hl-Completions">
{ this.completionRow(context.title, "hl-CompTitle") }
{ template.map(context.items, function (item) context.createRow(item)) }
</div>);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
},
completionRow: function completionRow(item, class) completionRow: function completionRow(item, class)
{ {
if (typeof icon == "function") if (typeof icon == "function")
@@ -93,7 +110,7 @@ const template = {
return <span class="hl-Number">{str}</span>; return <span class="hl-Number">{str}</span>;
case "string": case "string":
if (processStrings) if (processStrings)
str = <>{util.escapeString(str)}</>; str = str.quote();
return <span class="hl-String">{str}</span>; return <span class="hl-String">{str}</span>;
case "boolean": case "boolean":
return <span class="hl-Boolean">{str}</span>; return <span class="hl-Boolean">{str}</span>;
@@ -188,18 +205,12 @@ const template = {
// @param headers is an array of strings, the text for the header columns // @param headers is an array of strings, the text for the header columns
genericTable: function genericTable(items, format) genericTable: function genericTable(items, format)
{ {
// FIXME: Kludge. this.listCompleter(function (context) {
let context = new CompletionContext(""); context.filterFunc = null;
context.filterFunc = function (items) items;
if (format) if (format)
context.format = format; context.format = format;
return this.generic( context.completions = items;
<div class="hl-Completions"> });
{ this.completionRow(context.title, "hl-CompTitle") }
{
this.map(items, function (item) template.completionRow.call(context, { text: context.getKey({item: item}, "text"), item: item }))
}
</div>);
}, },
// returns a single row for a bookmark or history item // returns a single row for a bookmark or history item

View File

@@ -117,7 +117,7 @@ function CommandLine() //{{{
if (events.feedingKeys) if (events.feedingKeys)
return; return;
completionContext.reset(); completionContext.reset();
completionContext.fork("ex", 0, completion.ex, completion); completionContext.fork("ex", 0, completion, "ex");
commandline.setCompletions(completionContext.allItems); commandline.setCompletions(completionContext.allItems);
}); });
@@ -261,7 +261,7 @@ function CommandLine() //{{{
liberator.registerCallback("complete", modes.EX, function (str) { liberator.registerCallback("complete", modes.EX, function (str) {
completionContext.reset(); completionContext.reset();
completionContext.tabPressed = true; completionContext.tabPressed = true;
completionContext.fork("ex", 0, completion.ex, completion); completionContext.fork("ex", 0, completion, "ex");
return completionContext.allItems; return completionContext.allItems;
}); });
liberator.registerCallback("change", modes.EX, function (command) { liberator.registerCallback("change", modes.EX, function (command) {

View File

@@ -394,10 +394,7 @@ const util = { //{{{
{ {
if (Date.now() > endTime) if (Date.now() > endTime)
{ {
liberator.interrupted = false; liberator.threadYield(false, true);
liberator.threadYield();
if (liberator.interrupted)
throw new Error("Interrupted");
endTime = Date.now() + time; endTime = Date.now() + time;
} }
yield start++; yield start++;