]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Handle {,non-}pinned tabs separately in `gJ` and `gK`
[VimFx.git] / extension / lib / commands.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
4 # Copyright Wang Zhuochun 2013, 2014.
5 #
6 # This file is part of VimFx.
7 #
8 # VimFx is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # VimFx is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 ###
21
22 # This file defines all Normal mode commands. Commands that need to interact
23 # with web page content do so by running `vim._run(name)`, which invokes `name`
24 # in commands-frame.coffee.
25
26 # NOTE: Most tab related commands need to do their actual tab manipulations in
27 # the next tick (`utils.nextTick`) to work around bug 1200334.
28
29 help = require('./help')
30 hints = require('./hints')
31 prefs = require('./prefs')
32 utils = require('./utils')
33
34 commands = {}
35
36
37
38 commands.focus_location_bar = ({ vim }) ->
39 # This function works even if the Address Bar has been removed.
40 vim.window.focusAndSelectUrlBar()
41
42 commands.focus_search_bar = ({ vim, count }) ->
43 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
44 # has been removed. Therefore we first check if it exists.
45 if vim.window.BrowserSearch.searchBar
46 vim.window.BrowserSearch.webSearch()
47
48 helper_paste_and_go = (props, { vim }) ->
49 { gURLBar } = vim.window
50 gURLBar.value = vim.window.readFromClipboard()
51 gURLBar.handleCommand(new vim.window.KeyboardEvent('keydown', props))
52
53 commands.paste_and_go = helper_paste_and_go.bind(null, null)
54
55 commands.paste_and_go_in_tab = helper_paste_and_go.bind(null, {altKey: true})
56
57 commands.copy_current_url = ({ vim }) ->
58 utils.writeToClipboard(vim.window.gBrowser.currentURI.spec)
59
60 commands.go_up_path = ({ vim, count }) ->
61 vim._run('go_up_path', {count})
62
63 # Go up to root of the URL hierarchy.
64 commands.go_to_root = ({ vim }) ->
65 vim._run('go_to_root')
66
67 commands.go_home = ({ vim }) ->
68 vim.window.BrowserHome()
69
70 helper_go_history = (num, { vim, count = 1 }) ->
71 { SessionStore, gBrowser } = vim.window
72
73 # TODO: When Firefox 43 is released, only use the `.getSessionHistory`
74 # version and bump the minimut Firefox version.
75 if SessionStore.getSessionHistory
76 SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
77 { index } = sessionHistory
78 newIndex = index + num * count
79 newIndex = Math.max(newIndex, 0)
80 newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
81 gBrowser.gotoIndex(newIndex) unless newIndex == index
82 )
83 else
84 # Until then, fall back to a no-count version.
85 if num < 0 then gBrowser.goBack() else gBrowser.goForward()
86
87 commands.history_back = helper_go_history.bind(null, -1)
88
89 commands.history_forward = helper_go_history.bind(null, +1)
90
91 commands.reload = ({ vim }) ->
92 vim.window.BrowserReload()
93
94 commands.reload_force = ({ vim }) ->
95 vim.window.BrowserReloadSkipCache()
96
97 commands.reload_all = ({ vim }) ->
98 vim.window.gBrowser.reloadAllTabs()
99
100 commands.reload_all_force = ({ vim }) ->
101 for tab in vim.window.gBrowser.visibleTabs
102 gBrowser = tab.linkedBrowser
103 consts = gBrowser.webNavigation
104 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
105 gBrowser.reload(flags)
106 return
107
108 commands.stop = ({ vim }) ->
109 vim.window.BrowserStop()
110
111 commands.stop_all = ({ vim }) ->
112 for tab in vim.window.gBrowser.visibleTabs
113 tab.linkedBrowser.stop()
114 return
115
116
117
118 helper_scroll = (vim, method, type, direction, amount, property = null) ->
119 args = {
120 method, type, direction, amount, property
121 smooth: (prefs.root.get('general.smoothScroll') and
122 prefs.root.get("general.smoothScroll.#{ type }"))
123 }
124 reset = prefs.root.tmp(
125 'layout.css.scroll-behavior.spring-constant',
126 vim.options["smoothScroll.#{ type }.spring-constant"]
127 )
128 vim._run('scroll', args, reset)
129
130 helper_scrollByLinesX = (amount, { vim, count = 1 }) ->
131 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
132 helper_scroll(vim, 'scrollBy', 'lines', 'left', amount * distance * count * 5)
133
134 helper_scrollByLinesY = (amount, { vim, count = 1 }) ->
135 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
136 helper_scroll(vim, 'scrollBy', 'lines', 'top', amount * distance * count * 20)
137
138 helper_scrollByPagesY = (amount, { vim, count = 1 }) ->
139 helper_scroll(vim, 'scrollBy', 'pages', 'top', amount * count, 'clientHeight')
140
141 helper_scrollToX = (amount, { vim }) ->
142 helper_scroll(vim, 'scrollTo', 'other', 'left', amount, 'scrollLeftMax')
143
144 helper_scrollToY = (amount, { vim }) ->
145 helper_scroll(vim, 'scrollTo', 'other', 'top', amount, 'scrollTopMax')
146
147 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
148 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
149 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
150 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
151 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1)
152 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1)
153 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5)
154 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5)
155 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
156 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
157 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
158 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
159
160
161
162 commands.tab_new = ({ vim }) ->
163 utils.nextTick(vim.window, ->
164 vim.window.BrowserOpenTab()
165 )
166
167 commands.tab_duplicate = ({ vim }) ->
168 { gBrowser } = vim.window
169 utils.nextTick(vim.window, ->
170 gBrowser.duplicateTab(gBrowser.selectedTab)
171 )
172
173 absoluteTabIndex = (relativeIndex, gBrowser, { pinnedSeparate }) ->
174 tabs = gBrowser.visibleTabs
175 { selectedTab } = gBrowser
176
177 currentIndex = tabs.indexOf(selectedTab)
178 absoluteIndex = currentIndex + relativeIndex
179 numTabsTotal = tabs.length
180 numPinnedTabs = gBrowser._numPinnedTabs
181
182 [numTabs, min] = switch
183 when not pinnedSeparate then [numTabsTotal, 0]
184 when selectedTab.pinned then [numPinnedTabs, 0]
185 else [numTabsTotal - numPinnedTabs, numPinnedTabs]
186
187 wrap = (Math.abs(relativeIndex) == 1)
188 if wrap
189 absoluteIndex = min + (absoluteIndex - min) %% numTabs
190 else
191 absoluteIndex = Math.max(min, absoluteIndex)
192 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
193
194 return absoluteIndex
195
196 helper_switch_tab = (direction, { vim, count = 1 }) ->
197 { gBrowser } = vim.window
198 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
199 utils.nextTick(vim.window, ->
200 gBrowser.selectTabAtIndex(index)
201 )
202
203 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
204
205 commands.tab_select_next = helper_switch_tab.bind(null, +1)
206
207 helper_move_tab = (direction, { vim, count = 1 }) ->
208 { gBrowser } = vim.window
209 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
210 utils.nextTick(vim.window, ->
211 gBrowser.moveTabTo(gBrowser.selectedTab, index)
212 )
213
214 commands.tab_move_backward = helper_move_tab.bind(null, -1)
215
216 commands.tab_move_forward = helper_move_tab.bind(null, +1)
217
218 commands.tab_select_first = ({ vim, count = 1 }) ->
219 utils.nextTick(vim.window, ->
220 vim.window.gBrowser.selectTabAtIndex(count - 1)
221 )
222
223 commands.tab_select_first_non_pinned = ({ vim, count = 1 }) ->
224 firstNonPinned = vim.window.gBrowser._numPinnedTabs
225 utils.nextTick(vim.window, ->
226 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
227 )
228
229 commands.tab_select_last = ({ vim, count = 1 }) ->
230 utils.nextTick(vim.window, ->
231 vim.window.gBrowser.selectTabAtIndex(-count)
232 )
233
234 commands.tab_toggle_pinned = ({ vim }) ->
235 currentTab = vim.window.gBrowser.selectedTab
236 if currentTab.pinned
237 vim.window.gBrowser.unpinTab(currentTab)
238 else
239 vim.window.gBrowser.pinTab(currentTab)
240
241 commands.tab_close = ({ vim, count = 1}) ->
242 { gBrowser } = vim.window
243 return if gBrowser.selectedTab.pinned
244 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
245 utils.nextTick(vim.window, ->
246 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
247 gBrowser.removeTab(tab)
248 return
249 )
250
251 commands.tab_restore = ({ vim, count = 1 }) ->
252 utils.nextTick(vim.window, ->
253 vim.window.undoCloseTab() for [1..count] by 1
254 return
255 )
256
257 commands.tab_close_to_end = ({ vim }) ->
258 { gBrowser } = vim.window
259 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
260
261 commands.tab_close_other = ({ vim }) ->
262 { gBrowser } = vim.window
263 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
264
265
266
267 helper_follow = (name, vim, callback, count = null) ->
268 vim.markPageInteraction()
269
270 # Enter hints mode immediately, with an empty set of markers. The user might
271 # press keys before the `vim._run` callback is invoked. Those key presses
272 # should be handled in hints mode, not normal mode.
273 initialMarkers = []
274 storage = vim.enterMode('hints', initialMarkers, callback, count)
275
276 vim._run(name, null, ({ wrappers, viewport }) ->
277 # The user might have exited hints mode (and perhaps even entered it again)
278 # before this callback is invoked. If so, `storage.markers` has been
279 # cleared, or set to a new value. Only proceed if it is unchanged.
280 return unless storage.markers == initialMarkers
281
282 if wrappers.length > 0
283 markers = hints.injectHints(vim.window, wrappers, viewport, vim.options)
284 storage.markers = markers
285 else
286 vim.enterMode('normal')
287 )
288
289 helper_follow_clickable = ({ inTab, inBackground }, { vim, count = 1 }) ->
290 callback = (marker, timesLeft, keyStr) ->
291 isLast = (timesLeft == 1)
292 isLink = (marker.wrapper.type == 'link')
293
294 switch
295 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
296 inTab = not inTab
297 when keyStr.startsWith(vim.options.hints_toggle_in_background)
298 inTab = true
299 inBackground = not inBackground
300 else
301 unless isLast
302 inTab = true
303 inBackground = true
304
305 inTab = false unless isLink
306
307 if marker.type == 'text' or (isLink and not (inTab and inBackground))
308 isLast = true
309
310 { elementIndex } = marker.wrapper
311 vim._focusMarkerElement(elementIndex)
312
313 if inTab
314 utils.nextTick(vim.window, ->
315 utils.openTab(vim.window, marker.wrapper.href, {
316 inBackground
317 relatedToCurrent: true
318 })
319 )
320 else
321 vim._run('click_marker_element', {
322 elementIndex
323 preventTargetBlank: vim.options.prevent_target_blank
324 })
325
326 return not isLast
327
328 name = if inTab then 'follow_in_tab' else 'follow'
329 helper_follow(name, vim, callback, count)
330
331 # Follow links, focus text inputs and click buttons with hint markers.
332 commands.follow =
333 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
334
335 # Follow links in a new background tab with hint markers.
336 commands.follow_in_tab =
337 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
338
339 # Follow links in a new foreground tab with hint markers.
340 commands.follow_in_focused_tab =
341 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
342
343 # Like command_follow but multiple times.
344 commands.follow_multiple = (args) ->
345 args.count = Infinity
346 commands.follow(args)
347
348 # Copy the URL or text of a markable element to the system clipboard.
349 commands.follow_copy = ({ vim }) ->
350 callback = (marker) ->
351 { elementIndex } = marker.wrapper
352 property = switch marker.wrapper.type
353 when 'link' then 'href'
354 when 'typing' then 'value'
355 when 'contenteditable' then 'textContent'
356 vim._run('copy_marker_element', {elementIndex, property})
357 helper_follow('follow_copy', vim, callback)
358
359 # Focus element with hint markers.
360 commands.follow_focus = ({ vim }) ->
361 callback = (marker) ->
362 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
363 return helper_follow('follow_focus', vim, callback)
364
365 helper_follow_pattern = (type, { vim }) ->
366 options =
367 pattern_selector: vim.options.pattern_selector
368 pattern_attrs: vim.options.pattern_attrs
369 patterns: vim.options["#{ type }_patterns"]
370 vim._run('follow_pattern', {type, options})
371
372 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
373
374 commands.follow_next = helper_follow_pattern.bind(null, 'next')
375
376 # Focus last focused or first text input.
377 commands.focus_text_input = ({ vim, count }) ->
378 vim.markPageInteraction()
379 vim._run('focus_text_input', {count})
380
381 # Switch between text inputs or simulate `<tab>`.
382 helper_move_focus = (direction, { vim, uiEvent }) ->
383 if uiEvent
384 utils.moveFocus(direction)
385 else
386 vim.markPageInteraction()
387 vim._run('move_focus', {direction})
388
389 commands.focus_next = helper_move_focus.bind(null, +1)
390 commands.focus_previous = helper_move_focus.bind(null, -1)
391
392
393
394 findStorage = {lastSearchString: ''}
395
396 helper_find = (highlight, { vim }) ->
397 findBar = vim.window.gBrowser.getFindBar()
398
399 findBar.onFindCommand()
400 utils.focusElement(findBar._findField, {select: true})
401
402 return unless highlightButton = findBar.getElement('highlight')
403 if highlightButton.checked != highlight
404 highlightButton.click()
405
406 # Open the find bar, making sure that hightlighting is off.
407 commands.find = helper_find.bind(null, false)
408
409 # Open the find bar, making sure that hightlighting is on.
410 commands.find_highlight_all = helper_find.bind(null, true)
411
412 helper_find_again = (direction, { vim }) ->
413 findBar = vim.window.gBrowser.getFindBar()
414 if findStorage.lastSearchString.length > 0
415 findBar._findField.value = findStorage.lastSearchString
416 findBar.onFindAgainCommand(direction)
417 message = findBar._findStatusDesc.textContent
418 vim.notify(message) if message
419
420 commands.find_next = helper_find_again.bind(null, false)
421
422 commands.find_previous = helper_find_again.bind(null, true)
423
424
425
426 commands.enter_mode_ignore = ({ vim }) ->
427 vim.enterMode('ignore')
428
429 # Quote next keypress (pass it through to the page).
430 commands.quote = ({ vim, count = 1 }) ->
431 vim.enterMode('ignore', count)
432
433 # Display the Help Dialog.
434 commands.help = ({ vim }) ->
435 help.injectHelp(vim.window, vim._parent)
436
437 # Open and focus the Developer Toolbar.
438 commands.dev = ({ vim }) ->
439 vim.window.DeveloperToolbar.show(true) # `true` to focus.
440
441 commands.esc = ({ vim }) ->
442 vim._run('esc')
443 utils.blurActiveBrowserElement(vim)
444 help.removeHelp(vim.window)
445 vim.window.DeveloperToolbar.hide()
446 vim.window.gBrowser.getFindBar().close()
447 # TODO: Remove when Tab Groups have been removed.
448 vim.window.TabView?.hide()
449 hints.removeHints(vim.window) # Better safe than sorry.
450
451
452
453 module.exports = {
454 commands
455 findStorage
456 }
Imprint / Impressum