]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Merge branch 'master' into develop
[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.history_list = ({vim}) ->
92 {window} = vim
93 return unless menu = window.document.getElementById('backForwardMenu')
94 menu.openPopupAtScreen(
95 window.screenX + window.outerWidth / 2 - menu.clientWidth / 2,
96 window.screenY + window.outerHeight / 2 - menu.clientHeight / 2
97 )
98
99 commands.reload = ({vim}) ->
100 vim.window.BrowserReload()
101
102 commands.reload_force = ({vim}) ->
103 vim.window.BrowserReloadSkipCache()
104
105 commands.reload_all = ({vim}) ->
106 vim.window.gBrowser.reloadAllTabs()
107
108 commands.reload_all_force = ({vim}) ->
109 for tab in vim.window.gBrowser.visibleTabs
110 gBrowser = tab.linkedBrowser
111 consts = gBrowser.webNavigation
112 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
113 gBrowser.reload(flags)
114 return
115
116 commands.stop = ({vim}) ->
117 vim.window.BrowserStop()
118
119 commands.stop_all = ({vim}) ->
120 for tab in vim.window.gBrowser.visibleTabs
121 tab.linkedBrowser.stop()
122 return
123
124
125
126 helper_scroll = (vim, args...) ->
127 [method, type, directions, amounts, properties = null, name = 'scroll'] = args
128 options = {
129 method, type, directions, amounts, properties
130 smooth: (prefs.root.get('general.smoothScroll') and
131 prefs.root.get("general.smoothScroll.#{type}"))
132 }
133 reset = prefs.root.tmp(
134 'layout.css.scroll-behavior.spring-constant',
135 vim.options["smoothScroll.#{type}.spring-constant"]
136 )
137 vim._run(name, options, reset)
138
139
140 helper_scrollByLinesX = (amount, {vim, count = 1}) ->
141 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
142 helper_scroll(vim, 'scrollBy', 'lines', ['left'],
143 [amount * distance * count * 5])
144
145 helper_scrollByLinesY = (amount, {vim, count = 1}) ->
146 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
147 helper_scroll(vim, 'scrollBy', 'lines', ['top'],
148 [amount * distance * count * 20])
149
150 helper_scrollByPagesY = (amount, {vim, count = 1}) ->
151 helper_scroll(vim, 'scrollBy', 'pages', ['top'],
152 [amount * count], ['clientHeight'])
153
154 helper_scrollToX = (amount, {vim}) ->
155 helper_scroll(vim, 'scrollTo', 'other', ['left'], [amount], ['scrollLeftMax'])
156 helper_mark_last_scroll_position(vim)
157
158 helper_scrollToY = (amount, {vim}) ->
159 helper_scroll(vim, 'scrollTo', 'other', ['top'], [amount], ['scrollTopMax'])
160 helper_mark_last_scroll_position(vim)
161
162 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
163 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
164 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
165 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
166 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1)
167 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1)
168 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5)
169 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5)
170 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
171 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
172 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
173 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
174
175 helper_mark_last_scroll_position = (vim) ->
176 keyStr = vim.options.last_scroll_position_mark
177 vim._run('mark_scroll_position', {keyStr, notify: false})
178
179 commands.mark_scroll_position = ({vim}) ->
180 vim.enterMode('marks', (keyStr) -> vim._run('mark_scroll_position', {keyStr}))
181
182 commands.scroll_to_mark = ({vim}) ->
183 vim.enterMode('marks', (keyStr) ->
184 unless keyStr == vim.options.last_scroll_position_mark
185 helper_mark_last_scroll_position(vim)
186 helper_scroll(vim, 'scrollTo', 'other', ['top', 'left'], keyStr,
187 ['scrollTopMax', 'scrollLeftMax'], 'scroll_to_mark')
188 )
189
190
191
192 commands.tab_new = ({vim}) ->
193 utils.nextTick(vim.window, ->
194 vim.window.BrowserOpenTab()
195 )
196
197 commands.tab_duplicate = ({vim}) ->
198 {gBrowser} = vim.window
199 utils.nextTick(vim.window, ->
200 gBrowser.duplicateTab(gBrowser.selectedTab)
201 )
202
203 absoluteTabIndex = (relativeIndex, gBrowser, {pinnedSeparate}) ->
204 tabs = gBrowser.visibleTabs
205 {selectedTab} = gBrowser
206
207 currentIndex = tabs.indexOf(selectedTab)
208 absoluteIndex = currentIndex + relativeIndex
209 numTabsTotal = tabs.length
210 numPinnedTabs = gBrowser._numPinnedTabs
211
212 [numTabs, min] = switch
213 when not pinnedSeparate then [numTabsTotal, 0]
214 when selectedTab.pinned then [numPinnedTabs, 0]
215 else [numTabsTotal - numPinnedTabs, numPinnedTabs]
216
217 # Wrap _once_ if at one of the ends of the tab bar and cannot move in the
218 # current direction.
219 if (relativeIndex < 0 and currentIndex == min) or
220 (relativeIndex > 0 and currentIndex == min + numTabs - 1)
221 if absoluteIndex < min
222 absoluteIndex += numTabs
223 else if absoluteIndex >= min + numTabs
224 absoluteIndex -= numTabs
225
226 absoluteIndex = Math.max(min, absoluteIndex)
227 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
228
229 return absoluteIndex
230
231 helper_switch_tab = (direction, {vim, count = 1}) ->
232 {gBrowser} = vim.window
233 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
234 utils.nextTick(vim.window, ->
235 gBrowser.selectTabAtIndex(index)
236 )
237
238 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
239
240 commands.tab_select_next = helper_switch_tab.bind(null, +1)
241
242 helper_move_tab = (direction, {vim, count = 1}) ->
243 {gBrowser} = vim.window
244 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
245 utils.nextTick(vim.window, ->
246 gBrowser.moveTabTo(gBrowser.selectedTab, index)
247 )
248
249 commands.tab_move_backward = helper_move_tab.bind(null, -1)
250
251 commands.tab_move_forward = helper_move_tab.bind(null, +1)
252
253 commands.tab_move_to_window = ({vim}) ->
254 {gBrowser} = vim.window
255 gBrowser.replaceTabWithWindow(gBrowser.selectedTab)
256
257 commands.tab_select_first = ({vim, count = 1}) ->
258 utils.nextTick(vim.window, ->
259 vim.window.gBrowser.selectTabAtIndex(count - 1)
260 )
261
262 commands.tab_select_first_non_pinned = ({vim, count = 1}) ->
263 firstNonPinned = vim.window.gBrowser._numPinnedTabs
264 utils.nextTick(vim.window, ->
265 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
266 )
267
268 commands.tab_select_last = ({vim, count = 1}) ->
269 utils.nextTick(vim.window, ->
270 vim.window.gBrowser.selectTabAtIndex(-count)
271 )
272
273 commands.tab_toggle_pinned = ({vim}) ->
274 currentTab = vim.window.gBrowser.selectedTab
275 if currentTab.pinned
276 vim.window.gBrowser.unpinTab(currentTab)
277 else
278 vim.window.gBrowser.pinTab(currentTab)
279
280 commands.tab_close = ({vim, count = 1}) ->
281 {gBrowser} = vim.window
282 return if gBrowser.selectedTab.pinned
283 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
284 utils.nextTick(vim.window, ->
285 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
286 gBrowser.removeTab(tab)
287 return
288 )
289
290 commands.tab_restore = ({vim, count = 1}) ->
291 utils.nextTick(vim.window, ->
292 vim.window.undoCloseTab() for [1..count] by 1
293 return
294 )
295
296 commands.tab_close_to_end = ({vim}) ->
297 {gBrowser} = vim.window
298 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
299
300 commands.tab_close_other = ({vim}) ->
301 {gBrowser} = vim.window
302 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
303
304
305
306 helper_follow = (name, vim, callback, count = null) ->
307 vim.markPageInteraction()
308
309 # Enter hints mode immediately, with an empty set of markers. The user might
310 # press keys before the `vim._run` callback is invoked. Those key presses
311 # should be handled in hints mode, not normal mode.
312 initialMarkers = []
313 storage = vim.enterMode('hints', initialMarkers, callback, count)
314
315 vim._run(name, null, ({wrappers, viewport}) ->
316 # The user might have exited hints mode (and perhaps even entered it again)
317 # before this callback is invoked. If so, `storage.markers` has been
318 # cleared, or set to a new value. Only proceed if it is unchanged.
319 return unless storage.markers == initialMarkers
320
321 if wrappers.length > 0
322 markers = hints.injectHints(vim.window, wrappers, viewport, vim.options)
323 storage.markers = markers
324 else
325 vim.enterMode('normal')
326 )
327
328 helper_follow_clickable = ({inTab, inBackground}, {vim, count = 1}) ->
329 callback = (marker, timesLeft, keyStr) ->
330 {type, elementIndex} = marker.wrapper
331 isLast = (timesLeft == 1)
332 isLink = (type == 'link')
333
334 switch
335 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
336 inTab = not inTab
337 when keyStr.startsWith(vim.options.hints_toggle_in_background)
338 inTab = true
339 inBackground = not inBackground
340 else
341 unless isLast
342 inTab = true
343 inBackground = true
344
345 inTab = false unless isLink
346
347 if type == 'text' or (isLink and not (inTab and inBackground))
348 isLast = true
349
350 vim._focusMarkerElement(elementIndex)
351
352 if inTab
353 utils.nextTick(vim.window, ->
354 utils.openTab(vim.window, marker.wrapper.href, {
355 inBackground
356 relatedToCurrent: true
357 })
358 )
359 else
360 vim._run('click_marker_element', {
361 elementIndex, type
362 preventTargetBlank: vim.options.prevent_target_blank
363 })
364
365 return not isLast
366
367 name = if inTab then 'follow_in_tab' else 'follow'
368 helper_follow(name, vim, callback, count)
369
370 # Follow links, focus text inputs and click buttons with hint markers.
371 commands.follow =
372 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
373
374 # Follow links in a new background tab with hint markers.
375 commands.follow_in_tab =
376 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
377
378 # Follow links in a new foreground tab with hint markers.
379 commands.follow_in_focused_tab =
380 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
381
382 # Follow links in a new window with hint markers.
383 commands.follow_in_window = ({vim}) ->
384 callback = (marker) ->
385 vim._focusMarkerElement(marker.wrapper.elementIndex)
386 vim.window.openLinkIn(marker.wrapper.href, 'window', {})
387 helper_follow('follow_in_tab', vim, callback)
388
389 # Like command_follow but multiple times.
390 commands.follow_multiple = (args) ->
391 args.count = Infinity
392 commands.follow(args)
393
394 # Copy the URL or text of a markable element to the system clipboard.
395 commands.follow_copy = ({vim}) ->
396 callback = (marker) ->
397 {elementIndex} = marker.wrapper
398 property = switch marker.wrapper.type
399 when 'link' then 'href'
400 when 'text' then 'value'
401 when 'contenteditable' then 'textContent'
402 vim._run('copy_marker_element', {elementIndex, property})
403 helper_follow('follow_copy', vim, callback)
404
405 # Focus element with hint markers.
406 commands.follow_focus = ({vim}) ->
407 callback = (marker) ->
408 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
409 return helper_follow('follow_focus', vim, callback)
410
411 helper_follow_pattern = (type, {vim}) ->
412 options =
413 pattern_selector: vim.options.pattern_selector
414 pattern_attrs: vim.options.pattern_attrs
415 patterns: vim.options["#{type}_patterns"]
416 vim._run('follow_pattern', {type, options})
417
418 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
419
420 commands.follow_next = helper_follow_pattern.bind(null, 'next')
421
422 # Focus last focused or first text input.
423 commands.focus_text_input = ({vim, count}) ->
424 vim.markPageInteraction()
425 vim._run('focus_text_input', {count})
426
427
428
429 findStorage = {lastSearchString: ''}
430
431 helper_find = ({highlight, linksOnly = false}, {vim}) ->
432 helper_mark_last_scroll_position(vim)
433 findBar = vim.window.gBrowser.getFindBar()
434
435 mode = if linksOnly then findBar.FIND_LINKS else findBar.FIND_NORMAL
436 findBar.startFind(mode)
437 utils.focusElement(findBar._findField, {select: true})
438
439 return if linksOnly
440 return unless highlightButton = findBar.getElement('highlight')
441 if highlightButton.checked != highlight
442 highlightButton.click()
443
444 # Open the find bar, making sure that hightlighting is off.
445 commands.find = helper_find.bind(null, {highlight: false})
446
447 # Open the find bar, making sure that hightlighting is on.
448 commands.find_highlight_all = helper_find.bind(null, {highlight: true})
449
450 # Open the find bar in links only mode.
451 commands.find_links_only = helper_find.bind(null, {linksOnly: true})
452
453 helper_find_again = (direction, {vim}) ->
454 findBar = vim.window.gBrowser.getFindBar()
455 return unless findStorage.lastSearchString.length > 0
456 helper_mark_last_scroll_position(vim)
457 findBar._findField.value = findStorage.lastSearchString
458 findBar.onFindAgainCommand(direction)
459 message = findBar._findStatusDesc.textContent
460 vim.notify(message) if message
461
462 commands.find_next = helper_find_again.bind(null, false)
463
464 commands.find_previous = helper_find_again.bind(null, true)
465
466
467
468 commands.window_new = ({vim}) ->
469 vim.window.OpenBrowserWindow()
470
471 commands.window_new_private = ({vim}) ->
472 vim.window.OpenBrowserWindow({private: true})
473
474 commands.enter_mode_ignore = ({vim}) ->
475 vim.enterMode('ignore')
476
477 # Quote next keypress (pass it through to the page).
478 commands.quote = ({vim, count = 1}) ->
479 vim.enterMode('ignore', count)
480
481 # Display the Help Dialog.
482 commands.help = ({vim}) ->
483 help.injectHelp(vim.window, vim._parent)
484
485 # Open and focus the Developer Toolbar.
486 commands.dev = ({vim}) ->
487 vim.window.DeveloperToolbar.show(true) # `true` to focus.
488
489 commands.esc = ({vim}) ->
490 vim._run('esc')
491 utils.blurActiveBrowserElement(vim)
492 help.removeHelp(vim.window)
493 vim.window.DeveloperToolbar.hide()
494 vim.window.gBrowser.getFindBar().close()
495 # TODO: Remove when Tab Groups have been removed.
496 vim.window.TabView?.hide()
497 hints.removeHints(vim.window) # Better safe than sorry.
498
499
500
501 module.exports = {
502 commands
503 findStorage
504 }
Imprint / Impressum