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

Clean up the new hints code a bit

This commit is contained in:
Kris Maglione
2008-10-30 20:51:19 +00:00
parent e9c1f01a16
commit 8928c81404
4 changed files with 69 additions and 106 deletions

View File

@@ -1807,33 +1807,27 @@ function Marks() //{{{
} }
} }
function isLocalMark(mark) function isLocalMark(mark) /^[a-z]$/.test(mark);
{ function isURLMark(mark) /^[A-Z0-9]$/.test(mark);
return /^[a-z]$/.test(mark);
}
function isURLMark(mark) function localMarkIter()
{ {
return /^[A-Z0-9]$/.test(mark); for (let [mark, value] in localMarks)
for (let [,val] in Iterator(value))
yield [mark, val];
} }
function getSortedMarks() function getSortedMarks()
{ {
var location = window.content.location.href;
var lmarks = [];
// local marks // local marks
for (let [mark, value] in Iterator(localMarks)) let location = window.content.location.href;
{ let lmarks = [i for (i in localMarkIter()) if (i[1].location == location)];
for (let [,val] in Iterator(value.filter(function (val) val.location == location)))
lmarks.push([mark, val]);
}
lmarks.sort(); lmarks.sort();
// URL marks // URL marks
// FIXME: why does umarks.sort() cause a "Component is not available = // FIXME: why does umarks.sort() cause a "Component is not available =
// NS_ERROR_NOT_AVAILABLE" exception when used here? // NS_ERROR_NOT_AVAILABLE" exception when used here?
var umarks = [[key, mark] for ([key, mark] in urlMarks)]; let umarks = [i for (i in urlMarks)];
umarks.sort(function (a, b) a[0].localeCompare(b[0])); umarks.sort(function (a, b) a[0].localeCompare(b[0]));
return lmarks.concat(umarks); return lmarks.concat(umarks);
@@ -2020,7 +2014,7 @@ function Marks() //{{{
if (isURLMark(mark)) if (isURLMark(mark))
{ {
var slice = urlMarks.get(mark); let slice = urlMarks.get(mark);
if (slice && slice.tab && slice.tab.linkedBrowser) if (slice && slice.tab && slice.tab.linkedBrowser)
{ {
if (slice.tab.parentNode != getBrowser().tabContainer) if (slice.tab.parentNode != getBrowser().tabContainer)
@@ -2050,16 +2044,17 @@ function Marks() //{{{
} }
else if (isLocalMark(mark)) else if (isLocalMark(mark))
{ {
var win = window.content; let win = window.content;
var slice = localMarks.get(mark) || []; let slice = localMarks.get(mark) || [];
for (let i = 0; i < slice.length; i++) for (let [,lmark] in Iterator(slice))
{ {
if (win.location.href == slice[i].location) if (win.location.href == mark.location)
{ {
liberator.log("Jumping to local mark: " + markToString(mark, slice[i]), 5); liberator.log("Jumping to local mark: " + markToString(mark, lmark), 5);
win.scrollTo(slice[i].position.x * win.scrollMaxX, slice[i].position.y * win.scrollMaxY); win.scrollTo(lmark.position.x * win.scrollMaxX, lmark.position.y * win.scrollMaxY);
ok = true; ok = true;
break;
} }
} }
} }

View File

@@ -783,38 +783,32 @@ function Completion() //{{{
// FIXME: items shouldn't be [[[a], b]], but [[a, b]] and only mapped if at all for bLCS --mst // FIXME: items shouldn't be [[[a], b]], but [[a, b]] and only mapped if at all for bLCS --mst
buffer: function buffer(filter) buffer: function buffer(filter)
{ {
var items = []; let items = [];
var num = getBrowser().browsers.length; // FIXME: liberator.has("tabs")
var title, url;
for (let i = 0; i < num; i++) for (let [i, browser] in tabs.browsers)
{ {
i = i + 1;
let title = "";
try try
{ {
title = getBrowser().getBrowserAtIndex(i).contentDocument.title; title = browser.contentDocument.title;
}
catch (e)
{
title = "";
} }
catch (e) {}
url = getBrowser().getBrowserAtIndex(i).contentDocument.location.href; let url = browser.contentDocument.location.href;
if (title.indexOf(filter) == -1 && url.indexOf(filter) == -1 &&
(i + 1).toString().indexOf(filter) == -1)
continue;
if (title.indexOf(filter) != -1 || url.indexOf(filter) != -1 || if (title.indexOf(filter) != -1 || url.indexOf(filter) != -1 ||
(i + 1).toString().indexOf(filter) != -1) String.indexOf(i, filter) != -1)
{ {
if (title == "") if (title == "")
title = "(Untitled)"; title = "(Untitled)";
items.push([[(i + 1) + ": " + title, (i + 1) + ": " + url], url]); items.push([[i + ": " + title, i + ": " + url], url]);
} }
} }
if (!filter) if (!filter)
return [0, items.map(function (i) [i[0][0], i[1]])]; return [0, items.map(function ([a, b]) [a[0], b])];
return [0, buildLongestCommonSubstring(items, filter)]; return [0, buildLongestCommonSubstring(items, filter)];
}, },

View File

@@ -34,6 +34,7 @@ function Hints() //{{{
var myModes = config.browserModes || [modes.NORMAL]; var myModes = config.browserModes || [modes.NORMAL];
var hintMode;
var submode = ""; // used for extended mode, can be "o", "t", "y", etc. var submode = ""; // used for extended mode, can be "o", "t", "y", etc.
var hintString = ""; // the typed string part of the hint is in this string var hintString = ""; // the typed string part of the hint is in this string
var hintNumber = 0; // only the numerical part of the hint var hintNumber = 0; // only the numerical part of the hint
@@ -52,22 +53,25 @@ function Hints() //{{{
// docs = { doc: document, start: start_index in hints[], end: end_index in hints[] } // docs = { doc: document, start: start_index in hints[], end: end_index in hints[] }
var docs = []; var docs = [];
const hintDescriptions = { const Mode = new Struct("prompt", "action", "extended");
a: "Save hint with prompt:", const hintModes = {
s: "Save hint:", ";": Mode("Focus hint", function (elem) buffer.focusElement(elem), true),
o: "Follow hint:", a: Mode("Save hint with prompt", function (elem) buffer.saveLink(elem, false)),
t: "Follow hint in a new tab:", s: Mode("Save hint", function (elem) buffer.saveLink(elem, true)),
b: "Follow hint in a background tab:", o: Mode("Follow hint", function (elem) buffer.followLink(elem, liberator.CURRENT_TAB)),
O: "Open location based on hint:", t: Mode("Follow hint in a new tab", function (elem) buffer.followLink(elem, liberator.NEW_TAB)),
T: "Open new tab based on hint:", b: Mode("Follow hint in a background tab", function (elem) buffer.followLink(elem, liberator.NEW_BACKGROUND_TAB)),
v: "View hint source:", v: Mode("View hint source", function (elem, loc) buffer.viewSource(loc, false), true),
w: "Follow hint in a new window:", V: Mode("View hint source", function (elem, loc) buffer.viewSource(loc, true), true),
W: "Open new window based on hint:", w: Mode("Follow hint in a new window", function (elem) buffer.followLink(elem, liberator.NEW_WINDOW), true),
y: "Yank hint location:",
Y: "Yank hint description:" "?": Mode("Show information for hint", function (elem) buffer.showElementInfo(elem), true),
} O: Mode("Open location based on hint", function (elem, loc) commandline.open(":", "open " + loc, modes.EX)),
hintDescriptions[";"] = "Focus hint:"; T: Mode("Open new tab based on hint", function (elem, loc) commandline.open(":", "tabopen " + loc, modes.EX)),
hintDescriptions["?"] = "Show information for hint:"; W: Mode("Open new window based on hint", function (elem, loc) commandline.open(":", "winopen " + loc, modes.EX)),
y: Mode("Yank hint location", function (elem, loc) util.copyToClipboard(loc, true)),
Y: Mode("Yank hint description", function (elem) util.copyToClipboard(elem.textContent || "", true), true),
};
// reset all important variables // reset all important variables
function reset() function reset()
@@ -108,7 +112,7 @@ function Hints() //{{{
<span class="liberator-hint"/>, doc); <span class="liberator-hint"/>, doc);
var elem, tagname, text, span, rect; var elem, tagname, text, span, rect;
var res = buffer.evaluateXPath(options["hinttags"], doc, null, true); var res = buffer.evaluateXPath(options[hintMode.extended ? "extendedhinttags" : "hinttags"], doc, null, true);
var fragment = doc.createDocumentFragment(); var fragment = doc.createDocumentFragment();
var start = pageHints.length; var start = pageHints.length;
@@ -335,50 +339,19 @@ function Hints() //{{{
return false; return false;
} }
var timeout = followFirst ? 0 : 500; var timeout = followFirst || events.feedingKeys ? 0 : 500;
var activeIndex = (hintNumber ? hintNumber - 1 : 0); var activeIndex = (hintNumber ? hintNumber - 1 : 0);
var elem = validHints[activeIndex]; var elem = validHints[activeIndex];
var loc = elem.href || "";
switch (submode)
{
case ";": buffer.focusElement(elem); break;
case "a": buffer.saveLink(elem, false); break;
case "s": buffer.saveLink(elem, true); break;
case "o": buffer.followLink(elem, liberator.CURRENT_TAB); break;
case "t": buffer.followLink(elem, liberator.NEW_TAB); break;
case "b": buffer.followLink(elem, liberator.NEW_BACKGROUND_TAB); break;
case "v": buffer.viewSource(loc, false); break;
case "V": buffer.viewSource(loc, true); break;
case "w": buffer.followLink(elem, liberator.NEW_WINDOW); break;
// some modes need a timeout as they depend on the command line which is still blocked
// 500ms because otherwise `fad' would delete a tab, if "a" would make an "address" link unique
case "?": setTimeout(function () { buffer.showElementInfo(elem); }, timeout + 50); break;
case "y": setTimeout(function () { util.copyToClipboard(loc, true); }, timeout + 50); break;
case "Y": setTimeout(function () { util.copyToClipboard(elem.textContent || "", true); }, timeout + 50); break;
case "O": setTimeout(function () { commandline.open(":", "open " + loc, modes.EX); }, timeout); break;
case "T": setTimeout(function () { commandline.open(":", "tabopen " + loc, modes.EX); }, timeout); break;
case "W": setTimeout(function () { commandline.open(":", "winopen " + loc, modes.EX); }, timeout); break;
default:
liberator.echoerr("INTERNAL ERROR: unknown submode: " + submode);
}
removeHints(timeout); removeHints(timeout);
if (timeout == 0 || modes.isReplaying) if (timeout == 0)
{
// force a possible mode change, based on wheter an input field has focus // force a possible mode change, based on wheter an input field has focus
events.onFocusChange(); events.onFocusChange();
setTimeout(function () {
if (modes.extended & modes.HINTS) if (modes.extended & modes.HINTS)
modes.reset(false); modes.reset();
} hintMode.action(elem, elem.href || "");
else }, timeout);
{
setTimeout(function () {
if (modes.extended & modes.HINTS)
modes.reset();
}, timeout);
}
return true; return true;
} }
@@ -644,16 +617,16 @@ function Hints() //{{{
show: function (minor, filter, win) show: function (minor, filter, win)
{ {
let prompt = hintDescriptions[minor]; hintMode = hintModes[minor];
if (!prompt) if (!hintMode)
{ {
liberator.beep(); liberator.beep();
return; return;
} }
commandline.input(prompt, null, { onChange: onInput }); commandline.input(hintMode.prompt + ":", null, { onChange: onInput });
modes.extended = modes.HINTS; modes.extended = modes.HINTS;
submode = minor || "o"; // open is the default mode submode = minor;
hintString = filter || ""; hintString = filter || "";
hintNumber = 0; hintNumber = 0;
usedTab = false; usedTab = false;
@@ -752,7 +725,7 @@ function Hints() //{{{
return; return;
default: default:
if (/^[0-9]$/.test(key)) if (/^\d$/.test(key))
{ {
prevInput = "number"; prevInput = "number";

View File

@@ -923,21 +923,22 @@ function Tabs() //{{{
if (typeof reverse != "boolean") if (typeof reverse != "boolean")
reverse = false; reverse = false;
var matches = buffer.match(/^(\d+):?/); let matches = buffer.match(/^(\d+):?/);
if (matches) if (matches)
{ {
tabs.select(parseInt(matches[1], 10) - 1, false); // make it zero-based tabs.select(parseInt(matches[1], 10) - 1, false); // make it zero-based
return; return;
} }
var matches = []; matches = [];
var lowerBuffer = buffer.toLowerCase(); let lowerBuffer = buffer.toLowerCase();
var first = tabs.index() + (reverse ? 0 : 1); let first = tabs.index() + (reverse ? 0 : 1);
for (let i = 0; i < getBrowser().browsers.length; i++) let nbrowsers = getBrowser().browsers.length;
for (let [i,] in tabs.browsers)
{ {
var index = (i + first) % getBrowser().browsers.length; let index = (i + first) % nbrowsers;
var url = getBrowser().getBrowserAtIndex(index).contentDocument.location.href; let url = getBrowser().getBrowserAtIndex(index).contentDocument.location.href;
var title = getBrowser().getBrowserAtIndex(index).contentDocument.title.toLowerCase(); let title = getBrowser().getBrowserAtIndex(index).contentDocument.title.toLowerCase();
if (url == buffer) if (url == buffer)
{ {
tabs.select(index, false); tabs.select(index, false);