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