Skip to content

Commit

Permalink
Add g[, g] and a jump list
Browse files Browse the repository at this point in the history
Fixes #814.
  • Loading branch information
lydell committed Oct 2, 2016
1 parent 3c64fa2 commit 26f2a6b
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 11 deletions.
18 changes: 17 additions & 1 deletion documentation/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ eating your best hint char on most pages; see [The hint commands]).

[The hint commands]: #the-hint-commands--hints-mode

### `g[` and `g]`

Each time you use `gg`, `G`, `0`, `$`, `/`, `a/`, `g/`, `n`, `N` or `'`, the
current scroll position is recorded in a list just before the scrolling command
in question is performed. You can then travel back to the scroll positions in
that list by using the `g[` command. Went too far back? Use the `g]` to go
forward again.

If the current scroll position already exists in the list, it is moved to the
end. This way, repeating `g[` you will scroll back to old positions only once.

Both `g[` and `g]` go _count_ steps in the list.

This feature is inspired by Vim’s _jump list._ Some people prefer changing the
shortcuts to `<c-o>` and `<c-i>` to match Vim’s.

### Marks: `m` and `'`

Other than traditional scrolling, VimFx has _marks._ Press `m` followed by a
Expand All @@ -156,7 +172,7 @@ provides the `g/` shortcut instead.
Just like Vim, VimFx has a few special marks. These are set automatically.

- `'`: Pressing `''` takes you to the scroll position before the last `gg`, `G`,
`0`, `$`, `/`, `a/`, `g/`, `n`, `N` or `'`.
`0`, `$`, `/`, `a/`, `g/`, `n`, `N`, `'`, `g[` or `g]`.

- `/`: Pressing `'/` takes you to the scroll position before the last `/`, `a/`
or `g/`.
Expand Down
46 changes: 39 additions & 7 deletions extension/lib/commands-frame.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ commands.scroll = (args) ->
vim.state.scrollableElements.filterSuitableDefault()
viewportUtils.scroll(element, args)

commands.mark_scroll_position = ({vim, keyStr, notify = true}) ->
element = vim.state.scrollableElements.filterSuitableDefault()
vim.state.marks[keyStr] =
if element.ownerDocument.documentElement.localName == 'svg'
[element.ownerGlobal.scrollY, element.ownerGlobal.scrollX]
else
[element.scrollTop, element.scrollLeft]
commands.mark_scroll_position = (args) ->
{vim, keyStr, notify = true, addToJumpList = false} = args

vim.state.marks[keyStr] = vim.state.scrollableElements.getPageScrollPosition()

if addToJumpList
vim.addToJumpList()

if notify
vim.notify(translate('notification.mark_scroll_position.success', keyStr))

Expand All @@ -115,8 +116,39 @@ commands.scroll_to_mark = (args) ->
args.amounts = vim.state.marks[keyStr]
element = vim.state.scrollableElements.filterSuitableDefault()

commands.mark_scroll_position({
vim
keyStr: lastPositionMark
notify: false
addToJumpList: true
})
viewportUtils.scroll(element, args)

commands.scroll_to_position = (args) ->
{vim, extra: {count, direction, lastPositionMark}} = args

if direction == 'previous' and vim.state.jumpListIndex >= 0 and
vim.state.jumpListIndex == vim.state.jumpList.length - 1
vim.addToJumpList()

{jumpList, jumpListIndex} = vim.state
maxIndex = jumpList.length - 1

if (direction == 'previous' and jumpListIndex <= 0) or
(direction == 'next' and jumpListIndex >= maxIndex)
vim.notify(translate("notification.scroll_to_#{direction}_position.limit"))
return

index = jumpListIndex + count * (if direction == 'previous' then -1 else +1)
index = Math.max(index, 0)
index = Math.min(index, maxIndex)

args.amounts = jumpList[index]
element = vim.state.scrollableElements.filterSuitableDefault()

commands.mark_scroll_position({vim, keyStr: lastPositionMark, notify: false})
viewportUtils.scroll(element, args)
vim.state.jumpListIndex = index

helper_follow = (options, matcher, {vim, pass}) ->
{id, combine = true, selectors = FOLLOW_DEFAULT_SELECTORS} = options
Expand Down
20 changes: 17 additions & 3 deletions extension/lib/commands.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)

helper_mark_last_scroll_position = (vim) ->
keyStr = vim.options['scroll.last_position_mark']
vim._run('mark_scroll_position', {keyStr, notify: false})
vim._run('mark_scroll_position', {keyStr, notify: false, addToJumpList: true})

commands.mark_scroll_position = ({vim}) ->
vim._enterMode('marks', (keyStr) ->
Expand All @@ -254,14 +254,28 @@ commands.mark_scroll_position = ({vim}) ->
commands.scroll_to_mark = ({vim}) ->
vim._enterMode('marks', (keyStr) ->
helper_scroll(
vim, null, 'scrollTo', 'other', ['top', 'left'], [0, 0]
['scrollTopMax', 'scrollLeftMax'], 0, 'scroll_to_mark'
vim, null, 'scrollTo', 'other', ['left', 'top'], [0, 0]
['scrollLeftMax', 'scrollTopMax'], 0, 'scroll_to_mark'
{keyStr, lastPositionMark: vim.options['scroll.last_position_mark']}
)
vim.hideNotification()
)
vim.notify(translate('notification.scroll_to_mark.enter'))

helper_scroll_to_position = (direction, {vim, count = 1}) ->
lastPositionMark = vim.options['scroll.last_position_mark']
helper_scroll(
vim, null, 'scrollTo', 'other', ['left', 'top'], [0, 0]
['scrollLeftMax', 'scrollTopMax'], 0, 'scroll_to_position'
{count, direction, lastPositionMark}
)

commands.scroll_to_previous_position =
helper_scroll_to_position.bind(null, 'previous')

commands.scroll_to_next_position =
helper_scroll_to_position.bind(null, 'next')



commands.tab_new = ({vim}) ->
Expand Down
2 changes: 2 additions & 0 deletions extension/lib/defaults.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ shortcuts =
'$': 'scroll_to_right'
'm': 'mark_scroll_position'
"'": 'scroll_to_mark'
'g[': 'scroll_to_previous_position'
'g]': 'scroll_to_next_position'

'tabs':
't': 'tab_new'
Expand Down
7 changes: 7 additions & 0 deletions extension/lib/scrollable-elements.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ class ScrollableElements
@reject((element) => not @isScrollable(element))
return @largest ? @quirks(@window.document.documentElement)

getPageScrollPosition: ->
element = @filterSuitableDefault()
if element.ownerDocument.documentElement.localName == 'svg'
return [element.ownerGlobal.scrollX, element.ownerGlobal.scrollY]
else
return [element.scrollLeft, element.scrollTop]

module.exports = ScrollableElements
10 changes: 10 additions & 0 deletions extension/lib/vim-frame.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class VimFrame
hasInteraction: false
shouldRefocus: false
marks: {}
jumpList: []
jumpListIndex: -1
explicitBodyFocus: false
hasFocusedTextInput: false
lastFocusedTextInput: null
Expand Down Expand Up @@ -107,4 +109,12 @@ class VimFrame
utils.simulateMouseEvents(element, 'hover-end', browserOffset)
@state.lastHover.element = null

addToJumpList: ->
[newX, newY] = position = @state.scrollableElements.getPageScrollPosition()
jumpList = @state.jumpList[..@state.jumpListIndex]
.filter(([x, y]) -> not (x == newX and y == newY))
.concat([position])
@state.jumpList = jumpList
@state.jumpListIndex = jumpList.length - 1

module.exports = VimFrame
4 changes: 4 additions & 0 deletions extension/locale/de/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Zum linken Rand scrollen
mode.normal.scroll_to_right=Zum rechten Rand scrollen
mode.normal.mark_scroll_position=Scroll-Position markieren
mode.normal.scroll_to_mark=Zur Markierung scrollen
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Tabs
mode.normal.tab_new=Neuen Tab öffnen
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Scroll-Position markieren (Taste drücke
notification.mark_scroll_position.success=Markierung gesetzt: %S
notification.scroll_to_mark.enter=Zur Markierung scrollen (Taste drücken...)
notification.scroll_to_mark.none=Keine Markierung für: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Kein letzter Tab
notification.tab_select_oldest_unvisited.none=Keine unbesuchten Tabs
notification.tab_restore.none=Keine zuletzt geschlossene Tabs
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/en-US/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Scroll to the far left
mode.normal.scroll_to_right=Scroll to the far right
mode.normal.mark_scroll_position=Mark scroll position
mode.normal.scroll_to_mark=Scroll to mark
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Tabs
mode.normal.tab_new=New tab
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Mark scroll position (press a key…)
notification.mark_scroll_position.success=Mark set: %S
notification.scroll_to_mark.enter=Scroll to mark (press a key…)
notification.scroll_to_mark.none=No mark for: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=No most recent tab
notification.tab_select_oldest_unvisited.none=No unvisited tabs
notification.tab_restore.none=No recently closed tabs
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/es/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Desplazar al extremo izquierdo
mode.normal.scroll_to_right=Desplazar al extremo derecho
mode.normal.mark_scroll_position=Marcar posición de desplazamiento
mode.normal.scroll_to_mark=Desplazar hasta la marca
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Pestañas
mode.normal.tab_new=Nueva pestaña
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Marque la posición de desplazamiento (p
notification.mark_scroll_position.success=Se ha establecido la marca: %S
notification.scroll_to_mark.enter=Desplácese hasta la marca (pulse una tecla…)
notification.scroll_to_mark.none=No hay marca para: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=No hay pestaña más reciente
notification.tab_select_oldest_unvisited.none=No hay pestañas no visitadas
notification.tab_restore.none=No hay pestañas cerradas recientemente
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/fr/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Faire défiler jusqu'au bord gauche
mode.normal.scroll_to_right=Faire défiler jusqu'au bord droit
mode.normal.mark_scroll_position=Marquer la position actuelle
mode.normal.scroll_to_mark=Faire défiler jusqu'au marqueur
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position
category.tabs=Onglets
mode.normal.tab_new=Ouvrir un nouvel onglet
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Marquer la position actuelle (pressez un
notification.mark_scroll_position.success=Marqueur ajouté: %S
notification.scroll_to_mark.enter=Faire défiler jusqu'au marqueur (pressez une touche…)
notification.scroll_to_mark.none=Pas de marqueur pour: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Pas de dernier onglet ouvert
notification.tab_select_oldest_unvisited.none=Aucun onglet non visité
notification.tab_restore.none=Aucun onglet récemment fermé
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/id/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Gulung ke paling kiri
mode.normal.scroll_to_right=Gulung ke paling kanan
mode.normal.mark_scroll_position=Tandai posisi gulung
mode.normal.scroll_to_mark=Gulung ke Penanda
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Tab
mode.normal.tab_new=Tab baru
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Tandai posisi gulung (tekan sebuah tombo
notification.mark_scroll_position.success=Penanda dibuat: %S
notification.scroll_to_mark.enter=Gulung ke tanda (tekan sebuah tombol…)
notification.scroll_to_mark.none=Tidak ada penanda untuk: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Tidak ada tab terakhir
notification.tab_select_oldest_unvisited.none=Tidak ada tab yang belum dikunjungi
notification.tab_restore.none=Tidak ada tab terakhir ditutup
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/it/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Scorri all'estrema sinistra
mode.normal.scroll_to_right=Scorri all'estrema destra
mode.normal.mark_scroll_position=Salva la posizione nella pagina
mode.normal.scroll_to_mark=Vai alla posizione salvata
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Schede (tab)
mode.normal.tab_new=Apri una nuova scheda
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Marca la posizione nella pagina (premi u
notification.mark_scroll_position.success=Marcatura aggiunta: %S
notification.scroll_to_mark.enter=Vai alla marcatura (premi un tasto…)
notification.scroll_to_mark.none=Nessuna marcatura per: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Nessuna scheda più recente
notification.tab_select_oldest_unvisited.none=Nessuna scheda non visitata
notification.tab_restore.none=Nessuna scheda chiusa di recente
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/ja/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=左へスクロール
mode.normal.scroll_to_right=右へスクロール
mode.normal.mark_scroll_position=スクロール位置をマーク
mode.normal.scroll_to_mark=マークまでスクロール
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=タブの操作
mode.normal.tab_new=新規タブを開く
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=スクロール位置をマーク (キ
notification.mark_scroll_position.success=マークをセット: %S
notification.scroll_to_mark.enter=マークにスクロール (キーを押してください…)
notification.scroll_to_mark.none=マークがありません: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=直近のタブがありません
notification.tab_select_oldest_unvisited.none=未訪問のタブがありません
notification.tab_restore.none=最近閉じたタブがありません
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/nl/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Scroll helemaal naar links
mode.normal.scroll_to_right=Scroll helemaal naar rechts
mode.normal.mark_scroll_position=Scrollpositie opslaan
mode.normal.scroll_to_mark=Scroll naar opgeslagen scrollpositie
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Tabbladen
mode.normal.tab_new=Open een nieuw tabblad
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Onthoud scrollpositie (druk op een toets
notification.mark_scroll_position.success=Positie onthouden: %S
notification.scroll_to_mark.enter=Scroll naar positie (druk op een toets...)
notification.scroll_to_mark.none=Geen positie onthouden voor: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Geen meest recente tabs
notification.tab_select_oldest_unvisited.none=Geen onbezochte tabs
notification.tab_restore.none=Geen recent gesloten tabs
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/pt-BR/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Rolar para a esquerda até o fim
mode.normal.scroll_to_right=Rolar para a direita até o fim
mode.normal.mark_scroll_position=Marcar posição de rolagem
mode.normal.scroll_to_mark=Rolar até a marca
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Abas
mode.normal.tab_new=Nova aba
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Marcar posição de rolagem (pressione u
notification.mark_scroll_position.success=Marca definida: %S
notification.scroll_to_mark.enter=Rolar até a marca (pressione uma tecla…)
notification.scroll_to_mark.none=Nenhuma marca para: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Nenhuma aba mais recente
notification.tab_select_oldest_unvisited.none=Nenhuma aba não visitada
notification.tab_restore.none=Nenhuma aba recentemente fechada
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/ru/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Прокрутить влево до конца
mode.normal.scroll_to_right=Прокрутить вправо до конца
mode.normal.mark_scroll_position=Отметить позицию прокрутки
mode.normal.scroll_to_mark=Перейти к отметке
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=Вкладки
mode.normal.tab_new=Открыть новую вкладку
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Отметить позицию прок
notification.mark_scroll_position.success=Отметка установлена: %S
notification.scroll_to_mark.enter=Перейти к отметке (нажмите клавишу…)
notification.scroll_to_mark.none=Нет отметки: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=Нет послдених вкладок
notification.tab_select_oldest_unvisited.none=Нет непосещённых вкладок
notification.tab_restore.none=Нет недавно закрытых вкладок
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/sv-SE/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=Scrolla längst till vänster
mode.normal.scroll_to_right=Scrolla längst till höger
mode.normal.mark_scroll_position=Markera scrollposition
mode.normal.scroll_to_mark=Scrolla till markör
mode.normal.scroll_to_previous_position=Scrolla till föregående position
mode.normal.scroll_to_next_position=Scrolla till nästa position

category.tabs=Flikar
mode.normal.tab_new=Ny flik
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=Markera scrollposition (tryck på en tan
notification.mark_scroll_position.success=Markör satt: %S
notification.scroll_to_mark.enter=Scrolla till markör (tryck på en tangent…)
notification.scroll_to_mark.none=Ingen markör för: %S
notification.scroll_to_previous_position.limit=Ingen föregående scrollposition
notification.scroll_to_next_position.limit=Ingen nästa scrollposition
notification.tab_select_most_recent.none=Ingen senaste flik
notification.tab_select_oldest_unvisited.none=Inga obesökta flikar
notification.tab_restore.none=Inga nyligen stängda flikar
Expand Down
4 changes: 4 additions & 0 deletions extension/locale/zh-CN/vimfx.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ mode.normal.scroll_to_left=滚动到最左边
mode.normal.scroll_to_right=滚动到最右边
mode.normal.mark_scroll_position=标记滚动位置
mode.normal.scroll_to_mark=滚动到标记位置
mode.normal.scroll_to_previous_position=Scroll to previous position
mode.normal.scroll_to_next_position=Scroll to next position

category.tabs=标签页
mode.normal.tab_new=新建标签页
Expand Down Expand Up @@ -146,6 +148,8 @@ notification.mark_scroll_position.enter=标记滚动位置(按下一个按键…
notification.mark_scroll_position.success=设置标记: %S
notification.scroll_to_mark.enter=滚动到标记处(按下一个按键…)
notification.scroll_to_mark.none=没有发现标记: %S
notification.scroll_to_previous_position.limit=No previous scroll position
notification.scroll_to_next_position.limit=No next scroll position
notification.tab_select_most_recent.none=没有最近访问的标签页
notification.tab_select_oldest_unvisited.none=没有未访问的标签页
notification.tab_restore.none=无最近关闭的标签页
Expand Down
Loading

0 comments on commit 26f2a6b

Please sign in to comment.