From 9bdb4411af9a82be50e5fe6edcb1358ed0a26041 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Tue, 9 Aug 2011 14:50:51 -0400 Subject: [PATCH] Minor enhancements to / --- common/content/buffer.js | 30 +++++++++++++++++------------- common/content/marks.js | 33 ++++++++++++++++++++++++++------- pentadactyl/TODO | 1 + 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/common/content/buffer.js b/common/content/buffer.js index f4828eec..ba84392e 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -279,7 +279,7 @@ var Buffer = Module("buffer", { localStorePrototype: memoize({ instance: {}, get jumps() [], - jumpsIndex: 0 + jumpsIndex: -1 }), /** @@ -1268,10 +1268,12 @@ var Buffer = Module("buffer", { * null, to not alter the horizontal scroll offset. * @param {number|null} top The top absolute pixel offset. If * null, to not alter the vertical scroll offset. + * @param {string} reason The reason for the scroll event. See + * {@link marks.push}. @optional */ - scrollTo: function scrollTo(elem, left, top) { + scrollTo: function scrollTo(elem, left, top, reason) { if (elem.ownerDocument == buffer.focusedFrame.document) - marks.push(); + marks.push(reason); if (left != null) elem.scrollLeft = left; @@ -1288,7 +1290,7 @@ var Buffer = Module("buffer", { * Scrolls the currently given element horizontally. * * @param {Element} elem The element to scroll. - * @param {string} increment The increment by which to scroll. + * @param {string} unit The increment by which to scroll. * Possible values are: "columns", "pages" * @param {number} number The possibly fractional number of * increments to scroll. Positive values scroll to the right while @@ -1296,11 +1298,12 @@ var Buffer = Module("buffer", { * @throws {FailedAssertion} if scrolling is not possible in the * given direction. */ - scrollHorizontal: function scrollHorizontal(elem, increment, number) { + scrollHorizontal: function scrollHorizontal(elem, unit, number) { let fontSize = parseInt(util.computedStyle(elem).fontSize); - if (increment == "columns") + let increment; + if (unit == "columns") increment = fontSize; // Good enough, I suppose. - else if (increment == "pages") + else if (unit == "pages") increment = elem.clientWidth - fontSize; else throw Error(); @@ -1310,14 +1313,14 @@ var Buffer = Module("buffer", { let left = elem.dactylScrollDestX !== undefined ? elem.dactylScrollDestX : elem.scrollLeft; elem.dactylScrollDestX = undefined; - Buffer.scrollTo(elem, left + number * increment, null); + Buffer.scrollTo(elem, left + number * increment, null, "h-" + unit); }, /** * Scrolls the currently given element vertically. * * @param {Element} elem The element to scroll. - * @param {string} increment The increment by which to scroll. + * @param {string} unit The increment by which to scroll. * Possible values are: "lines", "pages" * @param {number} number The possibly fractional number of * increments to scroll. Positive values scroll upward while @@ -1325,11 +1328,12 @@ var Buffer = Module("buffer", { * @throws {FailedAssertion} if scrolling is not possible in the * given direction. */ - scrollVertical: function scrollVertical(elem, increment, number) { + scrollVertical: function scrollVertical(elem, unit, number) { let fontSize = parseInt(util.computedStyle(elem).lineHeight); - if (increment == "lines") + let increment; + if (unit == "lines") increment = fontSize; - else if (increment == "pages") + else if (unit == "pages") increment = elem.clientHeight - fontSize; else throw Error(); @@ -1339,7 +1343,7 @@ var Buffer = Module("buffer", { let top = elem.dactylScrollDestY !== undefined ? elem.dactylScrollDestY : elem.scrollTop; elem.dactylScrollDestY = undefined; - Buffer.scrollTo(elem, null, top + number * increment); + Buffer.scrollTo(elem, null, top + number * increment, "v-" + unit); }, /** diff --git a/common/content/marks.js b/common/content/marks.js index c15cbef4..99d42b8c 100644 --- a/common/content/marks.js +++ b/common/content/marks.js @@ -78,16 +78,32 @@ var Marks = Module("marks", { /** * Push the current buffer position onto the jump stack. + * + * @param {string} reason The reason for this scroll event. Multiple + * scroll events for the same reason are coalesced. @optional */ - push: function push() { + push: function push(reason) { + let store = buffer.localStore; + let jump = store.jumps[store.jumpsIndex]; + + if (reason && jump && jump.reason == reason) + return; + + let mark = this.add("'"); + if (!this.jumping) { - let mark = this.add("'"); - let store = buffer.localStore; - store.jumps[store.jumpsIndex++] = mark; - store.jumps.length = store.jumpsIndex; + store.jumps[++store.jumpsIndex] = { mark: mark, reason: reason }; + store.jumps.length = store.jumpsIndex + 1; + + if (store.jumps.length > this.maxJumps) { + store.jumps = store.jumps.slice(-this.maxJumps); + store.jumpsIndex = store.jumps.length - 1; + } } }, + maxJumps: 200, + /** * Jump to the given offset in the jump stack. * @@ -96,13 +112,16 @@ var Marks = Module("marks", { * @returns {number} The actual change in offset. */ jump: function jump(offset) { + let store = buffer.localStore; + if (offset < 0 && store.jumpsIndex == store.jumps.length - 1) + this.push(); + return this.withSavedValues(["jumping"], function _jump() { this.jumping = true; - let store = buffer.localStore; let idx = Math.constrain(store.jumpsIndex + offset, 0, store.jumps.length - 1); let orig = store.jumpsIndex; - if (idx in store.jumps && !dactyl.trapErrors("_scrollTo", this, store.jumps[idx])) + if (idx in store.jumps && !dactyl.trapErrors("_scrollTo", this, store.jumps[idx].mark)) store.jumpsIndex = idx; return store.jumpsIndex - orig; }); diff --git a/pentadactyl/TODO b/pentadactyl/TODO index 05f510e4..1e7a3be5 100644 --- a/pentadactyl/TODO +++ b/pentadactyl/TODO @@ -12,6 +12,7 @@ BUGS: - RC file is sourced once per window FEATURES: +9 Add more tests. 9 / should work as in Vim (i.e., save page positions as well as locations in the history list). 9 clean up error message codes and document