]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Merge pull request #561 from cbertoldi/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 = (event, { vim }) ->
49 { gURLBar } = vim.window
50 gURLBar.value = vim.window.readFromClipboard()
51 gURLBar.handleCommand(event)
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 })
327
328 return not isLast
329
330 name = if inTab then 'follow_in_tab' else 'follow'
331 helper_follow(name, vim, callback, count)
332
333 # Follow links, focus text inputs and click buttons with hint markers.
334 commands.follow =
335 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
336
337 # Follow links in a new background tab with hint markers.
338 commands.follow_in_tab =
339 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
340
341 # Follow links in a new foreground tab with hint markers.
342 commands.follow_in_focused_tab =
343 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
344
345 # Like command_follow but multiple times.
346 commands.follow_multiple = (args) ->
347 args.count = Infinity
348 commands.follow(args)
349
350 # Copy the URL or text of a markable element to the system clipboard.
351 commands.follow_copy = ({ vim }) ->
352 callback = (marker) ->
353 { elementIndex } = marker.wrapper
354 property = switch marker.wrapper.type
355 when 'link' then 'href'
356 when 'textInput' then 'value'
357 when 'contenteditable' then 'textContent'
358 vim._run('copy_marker_element', {elementIndex, property})
359 helper_follow('follow_copy', vim, callback)
360
361 # Focus element with hint markers.
362 commands.follow_focus = ({ vim }) ->
363 callback = (marker) ->
364 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
365 return helper_follow('follow_focus', vim, callback)
366
367 helper_follow_pattern = (type, { vim }) ->
368 options =
369 pattern_selector: vim.options.pattern_selector
370 pattern_attrs: vim.options.pattern_attrs
371 patterns: vim.options["#{ type }_patterns"]
372 vim._run('follow_pattern', {type, options})
373
374 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
375
376 commands.follow_next = helper_follow_pattern.bind(null, 'next')
377
378 # Focus last focused or first text input.
379 commands.focus_text_input = ({ vim, count }) ->
380 vim.markPageInteraction()
381 vim._run('focus_text_input', {count})
382
383 # Switch between text inputs or simulate `<tab>`.
384 helper_move_focus = (direction, { vim, uiEvent }) ->
385 if uiEvent
386 utils.moveFocus(direction)
387 else
388 vim.markPageInteraction()
389 vim._run('move_focus', {direction})
390
391 commands.focus_next = helper_move_focus.bind(null, +1)
392 commands.focus_previous = helper_move_focus.bind(null, -1)
393
394
395
396 findStorage = {lastSearchString: ''}
397
398 helper_find = (highlight, { vim }) ->
399 findBar = vim.window.gBrowser.getFindBar()
400
401 findBar.onFindCommand()
402 utils.focusElement(findBar._findField, {select: true})
403
404 return unless highlightButton = findBar.getElement('highlight')
405 if highlightButton.checked != highlight
406 highlightButton.click()
407
408 # Open the find bar, making sure that hightlighting is off.
409 commands.find = helper_find.bind(null, false)
410
411 # Open the find bar, making sure that hightlighting is on.
412 commands.find_highlight_all = helper_find.bind(null, true)
413
414 helper_find_again = (direction, { vim }) ->
415 findBar = vim.window.gBrowser.getFindBar()
416 if findStorage.lastSearchString.length > 0
417 findBar._findField.value = findStorage.lastSearchString
418 findBar.onFindAgainCommand(direction)
419 message = findBar._findStatusDesc.textContent
420 vim.notify(message) if message
421
422 commands.find_next = helper_find_again.bind(null, false)
423
424 commands.find_previous = helper_find_again.bind(null, true)
425
426
427
428 commands.enter_mode_ignore = ({ vim }) ->
429 vim.enterMode('ignore')
430
431 # Quote next keypress (pass it through to the page).
432 commands.quote = ({ vim, count = 1 }) ->
433 vim.enterMode('ignore', count)
434
435 # Display the Help Dialog.
436 commands.help = ({ vim }) ->
437 help.injectHelp(vim.window, vim._parent)
438
439 # Open and focus the Developer Toolbar.
440 commands.dev = ({ vim }) ->
441 vim.window.DeveloperToolbar.show(true) # `true` to focus.
442
443 commands.esc = ({ vim }) ->
444 vim._run('esc')
445 utils.blurActiveBrowserElement(vim)
446 help.removeHelp(vim.window)
447 vim.window.DeveloperToolbar.hide()
448 vim.window.gBrowser.getFindBar().close()
449 vim.window.TabView.hide()
450 hints.removeHints(vim.window) # Better safe than sorry.
451
452
453
454 module.exports = {
455 commands
456 findStorage
457 }
Imprint / Impressum