From 8928c81404a0c03d0c5185bd11bcb3af09e2e7e1 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Thu, 30 Oct 2008 20:51:19 +0000 Subject: [PATCH] Clean up the new hints code a bit --- content/buffer.js | 39 ++++++++----------- content/completion.js | 28 ++++++------- content/hints.js | 91 +++++++++++++++---------------------------- content/tabs.js | 17 ++++---- 4 files changed, 69 insertions(+), 106 deletions(-) diff --git a/content/buffer.js b/content/buffer.js index 996e71a4..3c41df14 100644 --- a/content/buffer.js +++ b/content/buffer.js @@ -1807,33 +1807,27 @@ function Marks() //{{{ } } - function isLocalMark(mark) - { - return /^[a-z]$/.test(mark); - } + function isLocalMark(mark) /^[a-z]$/.test(mark); + function isURLMark(mark) /^[A-Z0-9]$/.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() { - var location = window.content.location.href; - var lmarks = []; - // local marks - for (let [mark, value] in Iterator(localMarks)) - { - for (let [,val] in Iterator(value.filter(function (val) val.location == location))) - lmarks.push([mark, val]); - } + let location = window.content.location.href; + let lmarks = [i for (i in localMarkIter()) if (i[1].location == location)]; lmarks.sort(); // URL marks // FIXME: why does umarks.sort() cause a "Component is not available = // 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])); return lmarks.concat(umarks); @@ -2020,7 +2014,7 @@ function Marks() //{{{ if (isURLMark(mark)) { - var slice = urlMarks.get(mark); + let slice = urlMarks.get(mark); if (slice && slice.tab && slice.tab.linkedBrowser) { if (slice.tab.parentNode != getBrowser().tabContainer) @@ -2050,16 +2044,17 @@ function Marks() //{{{ } else if (isLocalMark(mark)) { - var win = window.content; - var slice = localMarks.get(mark) || []; + let win = window.content; + 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); - win.scrollTo(slice[i].position.x * win.scrollMaxX, slice[i].position.y * win.scrollMaxY); + liberator.log("Jumping to local mark: " + markToString(mark, lmark), 5); + win.scrollTo(lmark.position.x * win.scrollMaxX, lmark.position.y * win.scrollMaxY); ok = true; + break; } } } diff --git a/content/completion.js b/content/completion.js index 362ac57a..a7116bea 100644 --- a/content/completion.js +++ b/content/completion.js @@ -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 buffer: function buffer(filter) { - var items = []; - var num = getBrowser().browsers.length; - var title, url; + let items = []; + // FIXME: liberator.has("tabs") - for (let i = 0; i < num; i++) + for (let [i, browser] in tabs.browsers) { + i = i + 1; + let title = ""; try { - title = getBrowser().getBrowserAtIndex(i).contentDocument.title; - } - catch (e) - { - title = ""; + title = browser.contentDocument.title; } + catch (e) {} - url = getBrowser().getBrowserAtIndex(i).contentDocument.location.href; - - if (title.indexOf(filter) == -1 && url.indexOf(filter) == -1 && - (i + 1).toString().indexOf(filter) == -1) - continue; + let url = browser.contentDocument.location.href; if (title.indexOf(filter) != -1 || url.indexOf(filter) != -1 || - (i + 1).toString().indexOf(filter) != -1) + String.indexOf(i, filter) != -1) { if (title == "") title = "(Untitled)"; - items.push([[(i + 1) + ": " + title, (i + 1) + ": " + url], url]); + items.push([[i + ": " + title, i + ": " + url], url]); } } 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)]; }, diff --git a/content/hints.js b/content/hints.js index 6cdaf77d..1adf5f10 100644 --- a/content/hints.js +++ b/content/hints.js @@ -34,6 +34,7 @@ function Hints() //{{{ var myModes = config.browserModes || [modes.NORMAL]; + var hintMode; 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 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[] } var docs = []; - const hintDescriptions = { - a: "Save hint with prompt:", - s: "Save hint:", - o: "Follow hint:", - t: "Follow hint in a new tab:", - b: "Follow hint in a background tab:", - O: "Open location based on hint:", - T: "Open new tab based on hint:", - v: "View hint source:", - w: "Follow hint in a new window:", - W: "Open new window based on hint:", - y: "Yank hint location:", - Y: "Yank hint description:" - } - hintDescriptions[";"] = "Focus hint:"; - hintDescriptions["?"] = "Show information for hint:"; + const Mode = new Struct("prompt", "action", "extended"); + const hintModes = { + ";": Mode("Focus hint", function (elem) buffer.focusElement(elem), true), + a: Mode("Save hint with prompt", function (elem) buffer.saveLink(elem, false)), + s: Mode("Save hint", function (elem) buffer.saveLink(elem, true)), + o: Mode("Follow hint", function (elem) buffer.followLink(elem, liberator.CURRENT_TAB)), + t: Mode("Follow hint in a new tab", function (elem) buffer.followLink(elem, liberator.NEW_TAB)), + b: Mode("Follow hint in a background tab", function (elem) buffer.followLink(elem, liberator.NEW_BACKGROUND_TAB)), + v: Mode("View hint source", function (elem, loc) buffer.viewSource(loc, false), true), + V: Mode("View hint source", function (elem, loc) buffer.viewSource(loc, true), true), + w: Mode("Follow hint in a new window", function (elem) buffer.followLink(elem, liberator.NEW_WINDOW), true), + + "?": 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)), + T: Mode("Open new tab based on hint", function (elem, loc) commandline.open(":", "tabopen " + loc, modes.EX)), + 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 function reset() @@ -108,7 +112,7 @@ function Hints() //{{{ , doc); 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 start = pageHints.length; @@ -335,50 +339,19 @@ function Hints() //{{{ return false; } - var timeout = followFirst ? 0 : 500; + var timeout = followFirst || events.feedingKeys ? 0 : 500; var activeIndex = (hintNumber ? hintNumber - 1 : 0); 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); - if (timeout == 0 || modes.isReplaying) - { + if (timeout == 0) // force a possible mode change, based on wheter an input field has focus events.onFocusChange(); + setTimeout(function () { if (modes.extended & modes.HINTS) - modes.reset(false); - } - else - { - setTimeout(function () { - if (modes.extended & modes.HINTS) - modes.reset(); - }, timeout); - } - + modes.reset(); + hintMode.action(elem, elem.href || ""); + }, timeout); return true; } @@ -644,16 +617,16 @@ function Hints() //{{{ show: function (minor, filter, win) { - let prompt = hintDescriptions[minor]; - if (!prompt) + hintMode = hintModes[minor]; + if (!hintMode) { liberator.beep(); return; } - commandline.input(prompt, null, { onChange: onInput }); + commandline.input(hintMode.prompt + ":", null, { onChange: onInput }); modes.extended = modes.HINTS; - submode = minor || "o"; // open is the default mode + submode = minor; hintString = filter || ""; hintNumber = 0; usedTab = false; @@ -752,7 +725,7 @@ function Hints() //{{{ return; default: - if (/^[0-9]$/.test(key)) + if (/^\d$/.test(key)) { prevInput = "number"; diff --git a/content/tabs.js b/content/tabs.js index e6b5407a..cdc36245 100644 --- a/content/tabs.js +++ b/content/tabs.js @@ -923,21 +923,22 @@ function Tabs() //{{{ if (typeof reverse != "boolean") reverse = false; - var matches = buffer.match(/^(\d+):?/); + let matches = buffer.match(/^(\d+):?/); if (matches) { tabs.select(parseInt(matches[1], 10) - 1, false); // make it zero-based return; } - var matches = []; - var lowerBuffer = buffer.toLowerCase(); - var first = tabs.index() + (reverse ? 0 : 1); - for (let i = 0; i < getBrowser().browsers.length; i++) + matches = []; + let lowerBuffer = buffer.toLowerCase(); + let first = tabs.index() + (reverse ? 0 : 1); + let nbrowsers = getBrowser().browsers.length; + for (let [i,] in tabs.browsers) { - var index = (i + first) % getBrowser().browsers.length; - var url = getBrowser().getBrowserAtIndex(index).contentDocument.location.href; - var title = getBrowser().getBrowserAtIndex(index).contentDocument.title.toLowerCase(); + let index = (i + first) % nbrowsers; + let url = getBrowser().getBrowserAtIndex(index).contentDocument.location.href; + let title = getBrowser().getBrowserAtIndex(index).contentDocument.title.toLowerCase(); if (url == buffer) { tabs.select(index, false);