]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Merge pull request #553 from akhodakivskiy/non-multi-process-event-quirks
[VimFx.git] / extension / lib / commands.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
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 prefs = require('./prefs')
31 utils = require('./utils')
32
33 commands = {}
34
35
36
37 commands.focus_location_bar = ({ vim }) ->
38 # This function works even if the Address Bar has been removed.
39 vim.window.focusAndSelectUrlBar()
40
41 commands.focus_search_bar = ({ vim, count }) ->
42 # The `.webSearch()` method opens a search engine in a tab if the Search Bar
43 # has been removed. Therefore we first check if it exists.
44 if vim.window.BrowserSearch.searchBar
45 vim.window.BrowserSearch.webSearch()
46
47 helper_paste_and_go = (event, { vim }) ->
48 { gURLBar } = vim.window
49 gURLBar.value = vim.window.readFromClipboard()
50 gURLBar.handleCommand(event)
51
52 commands.paste_and_go = helper_paste_and_go.bind(null, null)
53
54 commands.paste_and_go_in_tab = helper_paste_and_go.bind(null, {altKey: true})
55
56 commands.copy_current_url = ({ vim }) ->
57 utils.writeToClipboard(vim.window.gBrowser.currentURI.spec)
58
59 commands.go_up_path = ({ vim, count }) ->
60 vim._run('go_up_path', {count})
61
62 # Go up to root of the URL hierarchy.
63 commands.go_to_root = ({ vim }) ->
64 vim._run('go_to_root')
65
66 commands.go_home = ({ vim }) ->
67 vim.window.BrowserHome()
68
69 helper_go_history = (num, { vim, count = 1 }) ->
70 { SessionStore, gBrowser } = vim.window
71
72 # TODO: When Firefox 43 is released, only use the `.getSessionHistory`
73 # version and bump the minimut Firefox version.
74 if SessionStore.getSessionHistory
75 SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
76 { index } = sessionHistory
77 newIndex = index + num * count
78 newIndex = Math.max(newIndex, 0)
79 newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
80 gBrowser.gotoIndex(newIndex) unless newIndex == index
81 )
82 else
83 # Until then, fall back to a no-count version.
84 if num < 0 then gBrowser.goBack() else gBrowser.goForward()
85
86 commands.history_back = helper_go_history.bind(null, -1)
87
88 commands.history_forward = helper_go_history.bind(null, +1)
89
90 commands.reload = ({ vim }) ->
91 vim.window.BrowserReload()
92
93 commands.reload_force = ({ vim }) ->
94 vim.window.BrowserReloadSkipCache()
95
96 commands.reload_all = ({ vim }) ->
97 vim.window.gBrowser.reloadAllTabs()
98
99 commands.reload_all_force = ({ vim }) ->
100 for tab in vim.window.gBrowser.visibleTabs
101 gBrowser = tab.linkedBrowser
102 consts = gBrowser.webNavigation
103 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
104 gBrowser.reload(flags)
105 return
106
107 commands.stop = ({ vim }) ->
108 vim.window.BrowserStop()
109
110 commands.stop_all = ({ vim }) ->
111 for tab in vim.window.gBrowser.visibleTabs
112 tab.linkedBrowser.stop()
113 return
114
115
116
117 helper_scroll = (vim, method, type, direction, amount, property = null) ->
118 args = {
119 method, type, direction, amount, property
120 smooth: (prefs.root.get('general.smoothScroll') and
121 prefs.root.get("general.smoothScroll.#{ type }"))
122 }
123 reset = prefs.root.tmp(
124 'layout.css.scroll-behavior.spring-constant',
125 vim.options["smoothScroll.#{ type }.spring-constant"]
126 )
127 vim._run('scroll', args, reset)
128
129 helper_scrollByLinesX = (amount, { vim, count = 1 }) ->
130 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
131 helper_scroll(vim, 'scrollBy', 'lines', 'left', amount * distance * count * 5)
132
133 helper_scrollByLinesY = (amount, { vim, count = 1 }) ->
134 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
135 helper_scroll(vim, 'scrollBy', 'lines', 'top', amount * distance * count * 20)
136
137 helper_scrollByPagesY = (amount, { vim, count = 1 }) ->
138 helper_scroll(vim, 'scrollBy', 'pages', 'top', amount * count, 'clientHeight')
139
140 helper_scrollToX = (amount, { vim }) ->
141 helper_scroll(vim, 'scrollTo', 'other', 'left', amount, 'scrollLeftMax')
142
143 helper_scrollToY = (amount, { vim }) ->
144 helper_scroll(vim, 'scrollTo', 'other', 'top', amount, 'scrollTopMax')
145
146 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
147 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
148 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
149 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
150 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1)
151 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1)
152 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5)
153 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5)
154 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
155 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
156 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
157 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
158
159
160
161 commands.tab_new = ({ vim }) ->
162 utils.nextTick(vim.window, ->
163 vim.window.BrowserOpenTab()
164 )
165
166 commands.tab_duplicate = ({ vim }) ->
167 { gBrowser } = vim.window
168 utils.nextTick(vim.window, ->
169 gBrowser.duplicateTab(gBrowser.selectedTab)
170 )
171
172 absoluteTabIndex = (relativeIndex, gBrowser) ->
173 tabs = gBrowser.visibleTabs
174 { selectedTab } = gBrowser
175
176 currentIndex = tabs.indexOf(selectedTab)
177 absoluteIndex = currentIndex + relativeIndex
178 numTabs = tabs.length
179
180 wrap = (Math.abs(relativeIndex) == 1)
181 if wrap
182 absoluteIndex %%= numTabs
183 else
184 absoluteIndex = Math.max(0, absoluteIndex)
185 absoluteIndex = Math.min(absoluteIndex, numTabs - 1)
186
187 return absoluteIndex
188
189 helper_switch_tab = (direction, { vim, count = 1 }) ->
190 { gBrowser } = vim.window
191 utils.nextTick(vim.window, ->
192 gBrowser.selectTabAtIndex(absoluteTabIndex(direction * count, gBrowser))
193 )
194
195 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
196
197 commands.tab_select_next = helper_switch_tab.bind(null, +1)
198
199 helper_move_tab = (direction, { vim, count = 1 }) ->
200 { gBrowser } = vim.window
201 { selectedTab } = gBrowser
202 { pinned } = selectedTab
203
204 index = absoluteTabIndex(direction * count, gBrowser)
205
206 if index < gBrowser._numPinnedTabs
207 gBrowser.pinTab(selectedTab) unless pinned
208 else
209 gBrowser.unpinTab(selectedTab) if pinned
210
211 utils.nextTick(vim.window, ->
212 gBrowser.moveTabTo(selectedTab, index)
213 )
214
215 commands.tab_move_backward = helper_move_tab.bind(null, -1)
216
217 commands.tab_move_forward = helper_move_tab.bind(null, +1)
218
219 commands.tab_select_first = ({ vim }) ->
220 utils.nextTick(vim.window, ->
221 vim.window.gBrowser.selectTabAtIndex(0)
222 )
223
224 commands.tab_select_first_non_pinned = ({ vim }) ->
225 firstNonPinned = vim.window.gBrowser._numPinnedTabs
226 utils.nextTick(vim.window, ->
227 vim.window.gBrowser.selectTabAtIndex(firstNonPinned)
228 )
229
230 commands.tab_select_last = ({ vim }) ->
231 utils.nextTick(vim.window, ->
232 vim.window.gBrowser.selectTabAtIndex(-1)
233 )
234
235 commands.tab_toggle_pinned = ({ vim }) ->
236 currentTab = vim.window.gBrowser.selectedTab
237 if currentTab.pinned
238 vim.window.gBrowser.unpinTab(currentTab)
239 else
240 vim.window.gBrowser.pinTab(currentTab)
241
242 commands.tab_close = ({ vim, count = 1}) ->
243 { gBrowser } = vim.window
244 return if gBrowser.selectedTab.pinned
245 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
246 utils.nextTick(vim.window, ->
247 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
248 gBrowser.removeTab(tab)
249 return
250 )
251
252 commands.tab_restore = ({ vim, count = 1 }) ->
253 utils.nextTick(vim.window, ->
254 vim.window.undoCloseTab() for [1..count] by 1
255 return
256 )
257
258 commands.tab_close_to_end = ({ vim }) ->
259 { gBrowser } = vim.window
260 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
261
262 commands.tab_close_other = ({ vim }) ->
263 { gBrowser } = vim.window
264 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
265
266
267
268 helper_follow = (name, vim, callback, count = null) ->
269 vim.markPageInteraction()
270 vim._run(name, null, ({ wrappers, viewport }) ->
271 vim.enterMode('hints', wrappers, viewport, callback, count)
272 )
273
274 helper_follow_clickable = ({ inTab, inBackground }, { vim, count = 1 }) ->
275 callback = (marker, timesLeft, keyStr) ->
276 isLast = (timesLeft == 1)
277 isLink = (marker.wrapper.type == 'link')
278
279 switch
280 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
281 inTab = not inTab
282 when keyStr.startsWith(vim.options.hints_toggle_in_background)
283 inTab = true
284 inBackground = not inBackground
285 else
286 unless isLast
287 inTab = true
288 inBackground = true
289
290 inTab = false unless isLink
291
292 if marker.type == 'text' or (isLink and not (inTab and inBackground))
293 isLast = true
294
295 { elementIndex } = marker.wrapper
296 vim._focusMarkerElement(elementIndex)
297
298 if inTab
299 utils.nextTick(vim.window, ->
300 utils.openTab(vim.window, marker.wrapper.href, {
301 inBackground
302 relatedToCurrent: true
303 })
304 )
305 else
306 vim._run('click_marker_element', {
307 elementIndex
308 preventTargetBlank: vim.options.prevent_target_blank
309 })
310
311 return not isLast
312
313 name = if inTab then 'follow_in_tab' else 'follow'
314 helper_follow(name, vim, callback, count)
315
316 # Follow links, focus text inputs and click buttons with hint markers.
317 commands.follow =
318 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
319
320 # Follow links in a new background tab with hint markers.
321 commands.follow_in_tab =
322 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
323
324 # Follow links in a new foreground tab with hint markers.
325 commands.follow_in_focused_tab =
326 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
327
328 # Like command_follow but multiple times.
329 commands.follow_multiple = (args) ->
330 args.count = Infinity
331 commands.follow(args)
332
333 # Copy the URL or text of a markable element to the system clipboard.
334 commands.follow_copy = ({ vim }) ->
335 callback = (marker) ->
336 { elementIndex } = marker.wrapper
337 property = switch marker.wrapper.type
338 when 'link' then 'href'
339 when 'textInput' then 'value'
340 when 'contenteditable' then 'textContent'
341 vim._run('copy_marker_element', {elementIndex, property})
342 helper_follow('follow_copy', vim, callback)
343
344 # Focus element with hint markers.
345 commands.follow_focus = ({ vim }) ->
346 callback = (marker) ->
347 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
348 return helper_follow('follow_focus', vim, callback)
349
350 helper_follow_pattern = (type, { vim }) ->
351 options =
352 pattern_selector: vim.options.pattern_selector
353 pattern_attrs: vim.options.pattern_attrs
354 patterns: vim.options["#{ type }_patterns"]
355 vim._run('follow_pattern', {type, options})
356
357 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
358
359 commands.follow_next = helper_follow_pattern.bind(null, 'next')
360
361 # Focus last focused or first text input.
362 commands.focus_text_input = ({ vim, count }) ->
363 vim.markPageInteraction()
364 vim._run('focus_text_input', {count})
365
366 # Switch between text inputs or simulate `<tab>`.
367 helper_move_focus = (direction, { vim, isFrameEvent }) ->
368 if isFrameEvent
369 vim.markPageInteraction()
370 vim._run('move_focus', {direction})
371 else
372 utils.moveFocus(direction)
373
374 commands.focus_next = helper_move_focus.bind(null, +1)
375 commands.focus_previous = helper_move_focus.bind(null, -1)
376
377
378
379 findStorage = {lastSearchString: ''}
380
381 helper_find = (highlight, { vim }) ->
382 findBar = vim.window.gBrowser.getFindBar()
383
384 findBar.onFindCommand()
385 utils.focusElement(findBar._findField, {select: true})
386
387 return unless highlightButton = findBar.getElement('highlight')
388 if highlightButton.checked != highlight
389 highlightButton.click()
390
391 # Open the find bar, making sure that hightlighting is off.
392 commands.find = helper_find.bind(null, false)
393
394 # Open the find bar, making sure that hightlighting is on.
395 commands.find_highlight_all = helper_find.bind(null, true)
396
397 helper_find_again = (direction, { vim }) ->
398 findBar = vim.window.gBrowser.getFindBar()
399 if findStorage.lastSearchString.length > 0
400 findBar._findField.value = findStorage.lastSearchString
401 findBar.onFindAgainCommand(direction)
402 message = findBar._findStatusDesc.textContent
403 vim.notify(message) if message
404
405 commands.find_next = helper_find_again.bind(null, false)
406
407 commands.find_previous = helper_find_again.bind(null, true)
408
409
410
411 commands.enter_mode_ignore = ({ vim }) ->
412 vim.enterMode('ignore')
413
414 # Quote next keypress (pass it through to the page).
415 commands.quote = ({ vim, count = 1 }) ->
416 vim.enterMode('ignore', count)
417
418 # Display the Help Dialog.
419 commands.help = ({ vim }) ->
420 help.injectHelp(vim.window, vim._parent)
421
422 # Open and focus the Developer Toolbar.
423 commands.dev = ({ vim }) ->
424 vim.window.DeveloperToolbar.show(true) # `true` to focus.
425
426 commands.esc = ({ vim }) ->
427 vim._run('esc')
428 utils.blurActiveBrowserElement(vim)
429 help.removeHelp(vim.window)
430 vim.window.DeveloperToolbar.hide()
431 vim.window.gBrowser.getFindBar().close()
432 vim.window.TabView.hide()
433
434
435
436 module.exports = {
437 commands
438 findStorage
439 }
Imprint / Impressum