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