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