]> 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.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 _once_ if at one of the ends of the tab bar and cannot move in the
188 # current direction.
189 if (relativeIndex < 0 and currentIndex == min) or
190 (relativeIndex > 0 and currentIndex == min + numTabs - 1)
191 if absoluteIndex < min
192 absoluteIndex += numTabs
193 else if absoluteIndex >= min + numTabs
194 absoluteIndex -= numTabs
195
196 absoluteIndex = Math.max(min, absoluteIndex)
197 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
198
199 return absoluteIndex
200
201 helper_switch_tab = (direction, {vim, count = 1}) ->
202 {gBrowser} = vim.window
203 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
204 utils.nextTick(vim.window, ->
205 gBrowser.selectTabAtIndex(index)
206 )
207
208 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
209
210 commands.tab_select_next = helper_switch_tab.bind(null, +1)
211
212 helper_move_tab = (direction, {vim, count = 1}) ->
213 {gBrowser} = vim.window
214 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
215 utils.nextTick(vim.window, ->
216 gBrowser.moveTabTo(gBrowser.selectedTab, index)
217 )
218
219 commands.tab_move_backward = helper_move_tab.bind(null, -1)
220
221 commands.tab_move_forward = helper_move_tab.bind(null, +1)
222
223 commands.tab_select_first = ({vim, count = 1}) ->
224 utils.nextTick(vim.window, ->
225 vim.window.gBrowser.selectTabAtIndex(count - 1)
226 )
227
228 commands.tab_select_first_non_pinned = ({vim, count = 1}) ->
229 firstNonPinned = vim.window.gBrowser._numPinnedTabs
230 utils.nextTick(vim.window, ->
231 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
232 )
233
234 commands.tab_select_last = ({vim, count = 1}) ->
235 utils.nextTick(vim.window, ->
236 vim.window.gBrowser.selectTabAtIndex(-count)
237 )
238
239 commands.tab_toggle_pinned = ({vim}) ->
240 currentTab = vim.window.gBrowser.selectedTab
241 if currentTab.pinned
242 vim.window.gBrowser.unpinTab(currentTab)
243 else
244 vim.window.gBrowser.pinTab(currentTab)
245
246 commands.tab_close = ({vim, count = 1}) ->
247 {gBrowser} = vim.window
248 return if gBrowser.selectedTab.pinned
249 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
250 utils.nextTick(vim.window, ->
251 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
252 gBrowser.removeTab(tab)
253 return
254 )
255
256 commands.tab_restore = ({vim, count = 1}) ->
257 utils.nextTick(vim.window, ->
258 vim.window.undoCloseTab() for [1..count] by 1
259 return
260 )
261
262 commands.tab_close_to_end = ({vim}) ->
263 {gBrowser} = vim.window
264 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
265
266 commands.tab_close_other = ({vim}) ->
267 {gBrowser} = vim.window
268 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
269
270
271
272 helper_follow = (name, vim, callback, count = null) ->
273 vim.markPageInteraction()
274
275 # Enter hints mode immediately, with an empty set of markers. The user might
276 # press keys before the `vim._run` callback is invoked. Those key presses
277 # should be handled in hints mode, not normal mode.
278 initialMarkers = []
279 storage = vim.enterMode('hints', initialMarkers, callback, count)
280
281 vim._run(name, null, ({wrappers, viewport}) ->
282 # The user might have exited hints mode (and perhaps even entered it again)
283 # before this callback is invoked. If so, `storage.markers` has been
284 # cleared, or set to a new value. Only proceed if it is unchanged.
285 return unless storage.markers == initialMarkers
286
287 if wrappers.length > 0
288 markers = hints.injectHints(vim.window, wrappers, viewport, vim.options)
289 storage.markers = markers
290 else
291 vim.enterMode('normal')
292 )
293
294 helper_follow_clickable = ({inTab, inBackground}, {vim, count = 1}) ->
295 callback = (marker, timesLeft, keyStr) ->
296 {type, elementIndex} = marker.wrapper
297 isLast = (timesLeft == 1)
298 isLink = (type == 'link')
299
300 switch
301 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
302 inTab = not inTab
303 when keyStr.startsWith(vim.options.hints_toggle_in_background)
304 inTab = true
305 inBackground = not inBackground
306 else
307 unless isLast
308 inTab = true
309 inBackground = true
310
311 inTab = false unless isLink
312
313 if type == 'text' or (isLink and not (inTab and inBackground))
314 isLast = true
315
316 vim._focusMarkerElement(elementIndex)
317
318 if inTab
319 utils.nextTick(vim.window, ->
320 utils.openTab(vim.window, marker.wrapper.href, {
321 inBackground
322 relatedToCurrent: true
323 })
324 )
325 else
326 vim._run('click_marker_element', {
327 elementIndex, type
328 preventTargetBlank: vim.options.prevent_target_blank
329 })
330
331 return not isLast
332
333 name = if inTab then 'follow_in_tab' else 'follow'
334 helper_follow(name, vim, callback, count)
335
336 # Follow links, focus text inputs and click buttons with hint markers.
337 commands.follow =
338 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
339
340 # Follow links in a new background tab with hint markers.
341 commands.follow_in_tab =
342 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
343
344 # Follow links in a new foreground tab with hint markers.
345 commands.follow_in_focused_tab =
346 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
347
348 # Like command_follow but multiple times.
349 commands.follow_multiple = (args) ->
350 args.count = Infinity
351 commands.follow(args)
352
353 # Copy the URL or text of a markable element to the system clipboard.
354 commands.follow_copy = ({vim}) ->
355 callback = (marker) ->
356 {elementIndex} = marker.wrapper
357 property = switch marker.wrapper.type
358 when 'link' then 'href'
359 when 'text' then 'value'
360 when 'contenteditable' then 'textContent'
361 vim._run('copy_marker_element', {elementIndex, property})
362 helper_follow('follow_copy', vim, callback)
363
364 # Focus element with hint markers.
365 commands.follow_focus = ({vim}) ->
366 callback = (marker) ->
367 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
368 return helper_follow('follow_focus', vim, callback)
369
370 helper_follow_pattern = (type, {vim}) ->
371 options =
372 pattern_selector: vim.options.pattern_selector
373 pattern_attrs: vim.options.pattern_attrs
374 patterns: vim.options["#{type}_patterns"]
375 vim._run('follow_pattern', {type, options})
376
377 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
378
379 commands.follow_next = helper_follow_pattern.bind(null, 'next')
380
381 # Focus last focused or first text input.
382 commands.focus_text_input = ({vim, count}) ->
383 vim.markPageInteraction()
384 vim._run('focus_text_input', {count})
385
386
387
388 findStorage = {lastSearchString: ''}
389
390 helper_find = (highlight, {vim}) ->
391 findBar = vim.window.gBrowser.getFindBar()
392
393 findBar.onFindCommand()
394 utils.focusElement(findBar._findField, {select: true})
395
396 return unless highlightButton = findBar.getElement('highlight')
397 if highlightButton.checked != highlight
398 highlightButton.click()
399
400 # Open the find bar, making sure that hightlighting is off.
401 commands.find = helper_find.bind(null, false)
402
403 # Open the find bar, making sure that hightlighting is on.
404 commands.find_highlight_all = helper_find.bind(null, true)
405
406 helper_find_again = (direction, {vim}) ->
407 findBar = vim.window.gBrowser.getFindBar()
408 if findStorage.lastSearchString.length > 0
409 findBar._findField.value = findStorage.lastSearchString
410 findBar.onFindAgainCommand(direction)
411 message = findBar._findStatusDesc.textContent
412 vim.notify(message) if message
413
414 commands.find_next = helper_find_again.bind(null, false)
415
416 commands.find_previous = helper_find_again.bind(null, true)
417
418
419
420 commands.enter_mode_ignore = ({vim}) ->
421 vim.enterMode('ignore')
422
423 # Quote next keypress (pass it through to the page).
424 commands.quote = ({vim, count = 1}) ->
425 vim.enterMode('ignore', count)
426
427 # Display the Help Dialog.
428 commands.help = ({vim}) ->
429 help.injectHelp(vim.window, vim._parent)
430
431 # Open and focus the Developer Toolbar.
432 commands.dev = ({vim}) ->
433 vim.window.DeveloperToolbar.show(true) # `true` to focus.
434
435 commands.esc = ({vim}) ->
436 vim._run('esc')
437 utils.blurActiveBrowserElement(vim)
438 help.removeHelp(vim.window)
439 vim.window.DeveloperToolbar.hide()
440 vim.window.gBrowser.getFindBar().close()
441 # TODO: Remove when Tab Groups have been removed.
442 vim.window.TabView?.hide()
443 hints.removeHints(vim.window) # Better safe than sorry.
444
445
446
447 module.exports = {
448 commands
449 findStorage
450 }
Imprint / Impressum