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