Skip to content

Commit

Permalink
Make the H and L commands more reliable
Browse files Browse the repository at this point in the history
It seems like using `window.BrowserBack()` and `window.BrowserForward()` are the
most reliable ways of navigating the history, rather than having to wait for
`SessionStore.getSessionHistory()` to finish and then going to a certain history
index. This commit optimizes the case where the count is 1 to use the mentioned
functions instead. Some extensions also override those, so calling them results
in better interoperability.

(Hopefully/Likely) fixes #687.

To keep the code changes simple, the support for Firefox < 43 was dropped.
That's fine, since Firefox 44 has already been released.
  • Loading branch information
lydell committed Feb 3, 2016
1 parent 92b483c commit e0787aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
47 changes: 29 additions & 18 deletions extension/lib/commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,35 @@ commands.go_home = ({vim}) ->
vim.window.BrowserHome()

helper_go_history = (direction, {vim, count = 1}) ->
{SessionStore, gBrowser} = vim.window

# TODO: When Firefox 43 is released, only use the `.getSessionHistory`
# version and bump the minimum Firefox version.
if SessionStore.getSessionHistory
SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
{index} = sessionHistory
newIndex = index + count * (if direction == 'back' then -1 else 1)
newIndex = Math.max(newIndex, 0)
newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
if newIndex == index
vim.notify(translate("notification.history_#{direction}.limit"))
else
gBrowser.gotoIndex(newIndex)
)
else
# Until then, fall back to a no-count version.
if direction == 'back' then gBrowser.goBack() else gBrowser.goForward()
{window} = vim
{SessionStore, gBrowser} = window

if (direction == 'back' and not gBrowser.canGoBack) or
(direction == 'forward' and not gBrowser.canGoForward)
vim.notify(translate("notification.history_#{direction}.limit"))
return

# `SessionStore.getSessionHistory()` (used below to support counts) starts
# lots of asynchronous tasks internally, which is a bit unreliable, it has
# turned out. The primary use of the `history_back` and `history_forward`
# commands is to go _one_ step back or forward, though, so those cases are
# optimized to use more reliable ways of going back and forward. Also, some
# extensions override the following functions, so calling them also gives
# better interoperability.
if count == 1
if direction == 'back'
window.BrowserBack()
else
window.BrowserForward()
return

SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
{index} = sessionHistory
newIndex = index + count * (if direction == 'back' then -1 else 1)
newIndex = Math.max(newIndex, 0)
newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
gBrowser.gotoIndex(newIndex)
)

commands.history_back = helper_go_history.bind(null, 'back')

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "VimFx",
"version": "0.11.0",
"firefoxVersions": {
"min": "40.0",
"min": "43.0",
"max": "45.*"
},
"license": "GPL-3.0+",
Expand Down

0 comments on commit e0787aa

Please sign in to comment.