]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Add a command to open the recently closed tabs list
[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.history_list = ({vim}) ->
92 utils.openPopup(vim.window.document.getElementById('backForwardMenu'))
93
94 commands.reload = ({vim}) ->
95 vim.window.BrowserReload()
96
97 commands.reload_force = ({vim}) ->
98 vim.window.BrowserReloadSkipCache()
99
100 commands.reload_all = ({vim}) ->
101 vim.window.gBrowser.reloadAllTabs()
102
103 commands.reload_all_force = ({vim}) ->
104 for tab in vim.window.gBrowser.visibleTabs
105 gBrowser = tab.linkedBrowser
106 consts = gBrowser.webNavigation
107 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
108 gBrowser.reload(flags)
109 return
110
111 commands.stop = ({vim}) ->
112 vim.window.BrowserStop()
113
114 commands.stop_all = ({vim}) ->
115 for tab in vim.window.gBrowser.visibleTabs
116 tab.linkedBrowser.stop()
117 return
118
119
120
121 helper_scroll = (vim, args...) ->
122 [method, type, directions, amounts, properties = null, name = 'scroll'] = args
123 options = {
124 method, type, directions, amounts, properties
125 smooth: (prefs.root.get('general.smoothScroll') and
126 prefs.root.get("general.smoothScroll.#{type}"))
127 }
128 reset = prefs.root.tmp(
129 'layout.css.scroll-behavior.spring-constant',
130 vim.options["smoothScroll.#{type}.spring-constant"]
131 )
132 vim._run(name, options, reset)
133
134
135 helper_scrollByLinesX = (amount, {vim, count = 1}) ->
136 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
137 helper_scroll(vim, 'scrollBy', 'lines', ['left'],
138 [amount * distance * count * 5])
139
140 helper_scrollByLinesY = (amount, {vim, count = 1}) ->
141 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
142 helper_scroll(vim, 'scrollBy', 'lines', ['top'],
143 [amount * distance * count * 20])
144
145 helper_scrollByPagesY = (amount, {vim, count = 1}) ->
146 helper_scroll(vim, 'scrollBy', 'pages', ['top'],
147 [amount * count], ['clientHeight'])
148
149 helper_scrollToX = (amount, {vim}) ->
150 helper_scroll(vim, 'scrollTo', 'other', ['left'], [amount], ['scrollLeftMax'])
151 helper_mark_last_scroll_position(vim)
152
153 helper_scrollToY = (amount, {vim}) ->
154 helper_scroll(vim, 'scrollTo', 'other', ['top'], [amount], ['scrollTopMax'])
155 helper_mark_last_scroll_position(vim)
156
157 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
158 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
159 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
160 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
161 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1)
162 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1)
163 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5)
164 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5)
165 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
166 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
167 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
168 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
169
170 helper_mark_last_scroll_position = (vim) ->
171 keyStr = vim.options.last_scroll_position_mark
172 vim._run('mark_scroll_position', {keyStr, notify: false})
173
174 commands.mark_scroll_position = ({vim}) ->
175 vim.enterMode('marks', (keyStr) -> vim._run('mark_scroll_position', {keyStr}))
176
177 commands.scroll_to_mark = ({vim}) ->
178 vim.enterMode('marks', (keyStr) ->
179 unless keyStr == vim.options.last_scroll_position_mark
180 helper_mark_last_scroll_position(vim)
181 helper_scroll(vim, 'scrollTo', 'other', ['top', 'left'], keyStr,
182 ['scrollTopMax', 'scrollLeftMax'], 'scroll_to_mark')
183 )
184
185
186
187 commands.tab_new = ({vim}) ->
188 utils.nextTick(vim.window, ->
189 vim.window.BrowserOpenTab()
190 )
191
192 commands.tab_duplicate = ({vim}) ->
193 {gBrowser} = vim.window
194 utils.nextTick(vim.window, ->
195 gBrowser.duplicateTab(gBrowser.selectedTab)
196 )
197
198 absoluteTabIndex = (relativeIndex, gBrowser, {pinnedSeparate}) ->
199 tabs = gBrowser.visibleTabs
200 {selectedTab} = gBrowser
201
202 currentIndex = tabs.indexOf(selectedTab)
203 absoluteIndex = currentIndex + relativeIndex
204 numTabsTotal = tabs.length
205 numPinnedTabs = gBrowser._numPinnedTabs
206
207 [numTabs, min] = switch
208 when not pinnedSeparate then [numTabsTotal, 0]
209 when selectedTab.pinned then [numPinnedTabs, 0]
210 else [numTabsTotal - numPinnedTabs, numPinnedTabs]
211
212 # Wrap _once_ if at one of the ends of the tab bar and cannot move in the
213 # current direction.
214 if (relativeIndex < 0 and currentIndex == min) or
215 (relativeIndex > 0 and currentIndex == min + numTabs - 1)
216 if absoluteIndex < min
217 absoluteIndex += numTabs
218 else if absoluteIndex >= min + numTabs
219 absoluteIndex -= numTabs
220
221 absoluteIndex = Math.max(min, absoluteIndex)
222 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
223
224 return absoluteIndex
225
226 helper_switch_tab = (direction, {vim, count = 1}) ->
227 {gBrowser} = vim.window
228 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
229 utils.nextTick(vim.window, ->
230 gBrowser.selectTabAtIndex(index)
231 )
232
233 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
234
235 commands.tab_select_next = helper_switch_tab.bind(null, +1)
236
237 helper_move_tab = (direction, {vim, count = 1}) ->
238 {gBrowser} = vim.window
239 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
240 utils.nextTick(vim.window, ->
241 gBrowser.moveTabTo(gBrowser.selectedTab, index)
242 )
243
244 commands.tab_move_backward = helper_move_tab.bind(null, -1)
245
246 commands.tab_move_forward = helper_move_tab.bind(null, +1)
247
248 commands.tab_move_to_window = ({vim}) ->
249 {gBrowser} = vim.window
250 gBrowser.replaceTabWithWindow(gBrowser.selectedTab)
251
252 commands.tab_select_first = ({vim, count = 1}) ->
253 utils.nextTick(vim.window, ->
254 vim.window.gBrowser.selectTabAtIndex(count - 1)
255 )
256
257 commands.tab_select_first_non_pinned = ({vim, count = 1}) ->
258 firstNonPinned = vim.window.gBrowser._numPinnedTabs
259 utils.nextTick(vim.window, ->
260 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
261 )
262
263 commands.tab_select_last = ({vim, count = 1}) ->
264 utils.nextTick(vim.window, ->
265 vim.window.gBrowser.selectTabAtIndex(-count)
266 )
267
268 commands.tab_toggle_pinned = ({vim}) ->
269 currentTab = vim.window.gBrowser.selectedTab
270 if currentTab.pinned
271 vim.window.gBrowser.unpinTab(currentTab)
272 else
273 vim.window.gBrowser.pinTab(currentTab)
274
275 commands.tab_close = ({vim, count = 1}) ->
276 {gBrowser} = vim.window
277 return if gBrowser.selectedTab.pinned
278 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
279 utils.nextTick(vim.window, ->
280 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
281 gBrowser.removeTab(tab)
282 return
283 )
284
285 commands.tab_restore = ({vim, count = 1}) ->
286 utils.nextTick(vim.window, ->
287 vim.window.undoCloseTab() for [1..count] by 1
288 return
289 )
290
291 commands.tab_restore_list = ({vim}) ->
292 {window} = vim
293 fragment = window.RecentlyClosedTabsAndWindowsMenuUtils.getTabsFragment(
294 window, 'menuitem'
295 )
296 if fragment.childElementCount > 0
297 utils.openPopup(utils.injectTemporaryPopup(window.document, fragment))
298
299 commands.tab_close_to_end = ({vim}) ->
300 {gBrowser} = vim.window
301 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
302
303 commands.tab_close_other = ({vim}) ->
304 {gBrowser} = vim.window
305 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
306
307
308
309 helper_follow = (name, vim, callback, count = null) ->
310 vim.markPageInteraction()
311
312 # Enter hints mode immediately, with an empty set of markers. The user might
313 # press keys before the `vim._run` callback is invoked. Those key presses
314 # should be handled in hints mode, not normal mode.
315 initialMarkers = []
316 storage = vim.enterMode('hints', initialMarkers, callback, count)
317
318 vim._run(name, null, ({wrappers, viewport}) ->
319 # The user might have exited hints mode (and perhaps even entered it again)
320 # before this callback is invoked. If so, `storage.markers` has been
321 # cleared, or set to a new value. Only proceed if it is unchanged.
322 return unless storage.markers == initialMarkers
323
324 if wrappers.length > 0
325 markers = hints.injectHints(vim.window, wrappers, viewport, vim.options)
326 storage.markers = markers
327 else
328 vim.enterMode('normal')
329 )
330
331 helper_follow_clickable = ({inTab, inBackground}, {vim, count = 1}) ->
332 callback = (marker, timesLeft, keyStr) ->
333 {type, elementIndex} = marker.wrapper
334 isLast = (timesLeft == 1)
335 isLink = (type == 'link')
336
337 switch
338 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
339 inTab = not inTab
340 when keyStr.startsWith(vim.options.hints_toggle_in_background)
341 inTab = true
342 inBackground = not inBackground
343 else
344 unless isLast
345 inTab = true
346 inBackground = true
347
348 inTab = false unless isLink
349
350 if type == 'text' or (isLink and not (inTab and inBackground))
351 isLast = true
352
353 vim._focusMarkerElement(elementIndex)
354
355 if inTab
356 utils.nextTick(vim.window, ->
357 utils.openTab(vim.window, marker.wrapper.href, {
358 inBackground
359 relatedToCurrent: true
360 })
361 )
362 else
363 vim._run('click_marker_element', {
364 elementIndex, type
365 preventTargetBlank: vim.options.prevent_target_blank
366 })
367
368 return not isLast
369
370 name = if inTab then 'follow_in_tab' else 'follow'
371 helper_follow(name, vim, callback, count)
372
373 # Follow links, focus text inputs and click buttons with hint markers.
374 commands.follow =
375 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
376
377 # Follow links in a new background tab with hint markers.
378 commands.follow_in_tab =
379 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
380
381 # Follow links in a new foreground tab with hint markers.
382 commands.follow_in_focused_tab =
383 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
384
385 # Follow links in a new window with hint markers.
386 commands.follow_in_window = ({vim}) ->
387 callback = (marker) ->
388 vim._focusMarkerElement(marker.wrapper.elementIndex)
389 vim.window.openLinkIn(marker.wrapper.href, 'window', {})
390 helper_follow('follow_in_tab', vim, callback)
391
392 # Like command_follow but multiple times.
393 commands.follow_multiple = (args) ->
394 args.count = Infinity
395 commands.follow(args)
396
397 # Copy the URL or text of a markable element to the system clipboard.
398 commands.follow_copy = ({vim}) ->
399 callback = (marker) ->
400 {elementIndex} = marker.wrapper
401 property = switch marker.wrapper.type
402 when 'link' then 'href'
403 when 'text' then 'value'
404 when 'contenteditable' then 'textContent'
405 vim._run('copy_marker_element', {elementIndex, property})
406 helper_follow('follow_copy', vim, callback)
407
408 # Focus element with hint markers.
409 commands.follow_focus = ({vim}) ->
410 callback = (marker) ->
411 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
412 return helper_follow('follow_focus', vim, callback)
413
414 helper_follow_pattern = (type, {vim}) ->
415 options =
416 pattern_selector: vim.options.pattern_selector
417 pattern_attrs: vim.options.pattern_attrs
418 patterns: vim.options["#{type}_patterns"]
419 vim._run('follow_pattern', {type, options})
420
421 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
422
423 commands.follow_next = helper_follow_pattern.bind(null, 'next')
424
425 # Focus last focused or first text input.
426 commands.focus_text_input = ({vim, count}) ->
427 vim.markPageInteraction()
428 vim._run('focus_text_input', {count})
429
430
431
432 findStorage = {lastSearchString: ''}
433
434 helper_find = ({highlight, linksOnly = false}, {vim}) ->
435 helper_mark_last_scroll_position(vim)
436 findBar = vim.window.gBrowser.getFindBar()
437
438 mode = if linksOnly then findBar.FIND_LINKS else findBar.FIND_NORMAL
439 findBar.startFind(mode)
440 utils.focusElement(findBar._findField, {select: true})
441
442 return if linksOnly
443 return unless highlightButton = findBar.getElement('highlight')
444 if highlightButton.checked != highlight
445 highlightButton.click()
446
447 # Open the find bar, making sure that hightlighting is off.
448 commands.find = helper_find.bind(null, {highlight: false})
449
450 # Open the find bar, making sure that hightlighting is on.
451 commands.find_highlight_all = helper_find.bind(null, {highlight: true})
452
453 # Open the find bar in links only mode.
454 commands.find_links_only = helper_find.bind(null, {linksOnly: true})
455
456 helper_find_again = (direction, {vim}) ->
457 findBar = vim.window.gBrowser.getFindBar()
458 return unless findStorage.lastSearchString.length > 0
459 helper_mark_last_scroll_position(vim)
460 findBar._findField.value = findStorage.lastSearchString
461 findBar.onFindAgainCommand(direction)
462 message = findBar._findStatusDesc.textContent
463 vim.notify(message) if message
464
465 commands.find_next = helper_find_again.bind(null, false)
466
467 commands.find_previous = helper_find_again.bind(null, true)
468
469
470
471 commands.window_new = ({vim}) ->
472 vim.window.OpenBrowserWindow()
473
474 commands.window_new_private = ({vim}) ->
475 vim.window.OpenBrowserWindow({private: true})
476
477 commands.enter_mode_ignore = ({vim}) ->
478 vim.enterMode('ignore')
479
480 # Quote next keypress (pass it through to the page).
481 commands.quote = ({vim, count = 1}) ->
482 vim.enterMode('ignore', count)
483
484 # Display the Help Dialog.
485 commands.help = ({vim}) ->
486 help.injectHelp(vim.window, vim._parent)
487
488 # Open and focus the Developer Toolbar.
489 commands.dev = ({vim}) ->
490 vim.window.DeveloperToolbar.show(true) # `true` to focus.
491
492 commands.esc = ({vim}) ->
493 vim._run('esc')
494 utils.blurActiveBrowserElement(vim)
495 help.removeHelp(vim.window)
496 vim.window.DeveloperToolbar.hide()
497 vim.window.gBrowser.getFindBar().close()
498 # TODO: Remove when Tab Groups have been removed.
499 vim.window.TabView?.hide()
500 hints.removeHints(vim.window) # Better safe than sorry.
501
502
503
504 module.exports = {
505 commands
506 findStorage
507 }
Imprint / Impressum