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