]> git.gir.st - VimFx.git/blob - extension/lib/commands.coffee
Add Caret mode
[VimFx.git] / extension / lib / commands.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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 config = require('./config')
30 help = require('./help')
31 hints = require('./hints')
32 prefs = require('./prefs')
33 translate = require('./l10n')
34 utils = require('./utils')
35
36 {ContentClick} = Cu.import('resource:///modules/ContentClick.jsm', {})
37
38 commands = {}
39
40
41
42 commands.focus_location_bar = ({vim}) ->
43 vim.window.focusAndSelectUrlBar()
44
45 commands.focus_search_bar = ({vim, count}) ->
46 # The `.webSearch()` method opens a search engine in a tab if the search bar
47 # has been removed. Therefore we first check if it exists.
48 if vim.window.BrowserSearch.searchBar
49 vim.window.BrowserSearch.webSearch()
50 else
51 vim.notify(translate('notification.focus_search_bar.none'))
52
53 helper_paste_and_go = (props, {vim}) ->
54 {gURLBar} = vim.window
55 gURLBar.value = vim.window.readFromClipboard()
56 gURLBar.handleCommand(new vim.window.KeyboardEvent('keydown', props))
57
58 commands.paste_and_go = helper_paste_and_go.bind(null, null)
59
60 commands.paste_and_go_in_tab = helper_paste_and_go.bind(null, {altKey: true})
61
62 commands.copy_current_url = ({vim}) ->
63 utils.writeToClipboard(vim.window.gBrowser.currentURI.spec)
64 vim.notify(translate('notification.copy_current_url.success'))
65
66 commands.go_up_path = ({vim, count}) ->
67 vim._run('go_up_path', {count})
68
69 commands.go_to_root = ({vim}) ->
70 vim._run('go_to_root')
71
72 commands.go_home = ({vim}) ->
73 vim.window.BrowserHome()
74
75 helper_go_history = (direction, {vim, count = 1}) ->
76 {window} = vim
77 {SessionStore, gBrowser} = window
78
79 if (direction == 'back' and not gBrowser.canGoBack) or
80 (direction == 'forward' and not gBrowser.canGoForward)
81 vim.notify(translate("notification.history_#{direction}.limit"))
82 return
83
84 # `SessionStore.getSessionHistory()` (used below to support counts) starts
85 # lots of asynchronous tasks internally, which is a bit unreliable, it has
86 # turned out. The primary use of the `history_back` and `history_forward`
87 # commands is to go _one_ step back or forward, though, so those cases are
88 # optimized to use more reliable ways of going back and forward. Also, some
89 # extensions override the following functions, so calling them also gives
90 # better interoperability.
91 if count == 1
92 if direction == 'back'
93 window.BrowserBack()
94 else
95 window.BrowserForward()
96 return
97
98 SessionStore.getSessionHistory(gBrowser.selectedTab, (sessionHistory) ->
99 {index} = sessionHistory
100 newIndex = index + count * (if direction == 'back' then -1 else 1)
101 newIndex = Math.max(newIndex, 0)
102 newIndex = Math.min(newIndex, sessionHistory.entries.length - 1)
103 gBrowser.gotoIndex(newIndex)
104 )
105
106 commands.history_back = helper_go_history.bind(null, 'back')
107
108 commands.history_forward = helper_go_history.bind(null, 'forward')
109
110 commands.history_list = ({vim}) ->
111 menu = vim.window.document.getElementById('backForwardMenu')
112 utils.openPopup(menu)
113 if menu.childElementCount == 0
114 vim.notify(translate('notification.history_list.none'))
115
116 commands.reload = ({vim}) ->
117 vim.window.BrowserReload()
118
119 commands.reload_force = ({vim}) ->
120 vim.window.BrowserReloadSkipCache()
121
122 commands.reload_all = ({vim}) ->
123 vim.window.gBrowser.reloadAllTabs()
124
125 commands.reload_all_force = ({vim}) ->
126 for tab in vim.window.gBrowser.visibleTabs
127 gBrowser = tab.linkedBrowser
128 consts = gBrowser.webNavigation
129 flags = consts.LOAD_FLAGS_BYPASS_PROXY | consts.LOAD_FLAGS_BYPASS_CACHE
130 gBrowser.reload(flags)
131 return
132
133 commands.stop = ({vim}) ->
134 vim.window.BrowserStop()
135
136 commands.stop_all = ({vim}) ->
137 for tab in vim.window.gBrowser.visibleTabs
138 tab.linkedBrowser.stop()
139 return
140
141
142
143 helper_scroll = (vim, uiEvent, args...) ->
144 [
145 method, type, directions, amounts
146 properties = null, adjustment = 0, name = 'scroll'
147 ] = args
148 options = {
149 method, type, directions, amounts, properties, adjustment
150 smooth: (
151 prefs.root.get('general.smoothScroll') and
152 prefs.root.get("general.smoothScroll.#{type}")
153 )
154 }
155 reset = prefs.root.tmp(
156 'layout.css.scroll-behavior.spring-constant',
157 vim.options["smoothScroll.#{type}.spring-constant"]
158 )
159
160 helpScroll = help.getHelp(vim.window)?.querySelector('.wrapper')
161 if uiEvent or helpScroll
162 activeElement = helpScroll or utils.getActiveElement(vim.window)
163 if vim._state.scrollableElements.has(activeElement) or helpScroll
164 utils.scroll(activeElement, options)
165 reset()
166 return
167
168 vim._run(name, options, reset)
169
170
171 helper_scrollByLinesX = (amount, {vim, uiEvent, count = 1}) ->
172 distance = prefs.root.get('toolkit.scrollbox.horizontalScrollDistance')
173 helper_scroll(
174 vim, uiEvent, 'scrollBy', 'lines', ['left'], [amount * distance * count * 5]
175 )
176
177 helper_scrollByLinesY = (amount, {vim, uiEvent, count = 1}) ->
178 distance = prefs.root.get('toolkit.scrollbox.verticalScrollDistance')
179 helper_scroll(
180 vim, uiEvent, 'scrollBy', 'lines', ['top'], [amount * distance * count * 20]
181 )
182
183 helper_scrollByPagesY = (amount, type, {vim, uiEvent, count = 1}) ->
184 adjustment = vim.options["scroll.#{type}_page_adjustment"]
185 helper_scroll(
186 vim, uiEvent, 'scrollBy', 'pages', ['top'], [amount * count],
187 ['clientHeight'], adjustment
188 )
189
190 helper_scrollToX = (amount, {vim, uiEvent}) ->
191 helper_mark_last_scroll_position(vim)
192 helper_scroll(
193 vim, uiEvent, 'scrollTo', 'other', ['left'], [amount], ['scrollLeftMax']
194 )
195
196 helper_scrollToY = (amount, {vim, uiEvent}) ->
197 helper_mark_last_scroll_position(vim)
198 helper_scroll(
199 vim, uiEvent, 'scrollTo', 'other', ['top'], [amount], ['scrollTopMax']
200 )
201
202 commands.scroll_left = helper_scrollByLinesX.bind(null, -1)
203 commands.scroll_right = helper_scrollByLinesX.bind(null, +1)
204 commands.scroll_down = helper_scrollByLinesY.bind(null, +1)
205 commands.scroll_up = helper_scrollByLinesY.bind(null, -1)
206 commands.scroll_page_down = helper_scrollByPagesY.bind(null, +1, 'full')
207 commands.scroll_page_up = helper_scrollByPagesY.bind(null, -1, 'full')
208 commands.scroll_half_page_down = helper_scrollByPagesY.bind(null, +0.5, 'half')
209 commands.scroll_half_page_up = helper_scrollByPagesY.bind(null, -0.5, 'half')
210 commands.scroll_to_top = helper_scrollToY.bind(null, 0)
211 commands.scroll_to_bottom = helper_scrollToY.bind(null, Infinity)
212 commands.scroll_to_left = helper_scrollToX.bind(null, 0)
213 commands.scroll_to_right = helper_scrollToX.bind(null, Infinity)
214
215 helper_mark_last_scroll_position = (vim) ->
216 keyStr = vim.options['scroll.last_position_mark']
217 vim._run('mark_scroll_position', {keyStr, notify: false})
218
219 commands.mark_scroll_position = ({vim}) ->
220 vim.enterMode('marks', (keyStr) -> vim._run('mark_scroll_position', {keyStr}))
221 vim.notify(translate('notification.mark_scroll_position.enter'))
222
223 commands.scroll_to_mark = ({vim}) ->
224 vim.enterMode('marks', (keyStr) ->
225 unless keyStr == vim.options['scroll.last_position_mark']
226 helper_mark_last_scroll_position(vim)
227 helper_scroll(
228 vim, null, 'scrollTo', 'other', ['top', 'left'], keyStr,
229 ['scrollTopMax', 'scrollLeftMax'], 0, 'scroll_to_mark'
230 )
231 )
232 vim.notify(translate('notification.scroll_to_mark.enter'))
233
234
235
236 commands.tab_new = ({vim}) ->
237 utils.nextTick(vim.window, ->
238 vim.window.BrowserOpenTab()
239 )
240
241 commands.tab_new_after_current = ({vim}) ->
242 {window} = vim
243 newTabPosition = window.gBrowser.selectedTab._tPos + 1
244 utils.nextTick(window, ->
245 utils.listenOnce(window, 'TabOpen', (event) ->
246 newTab = event.originalTarget
247 window.gBrowser.moveTabTo(newTab, newTabPosition)
248 )
249 window.BrowserOpenTab()
250 )
251
252 commands.tab_duplicate = ({vim}) ->
253 {gBrowser} = vim.window
254 utils.nextTick(vim.window, ->
255 gBrowser.duplicateTab(gBrowser.selectedTab)
256 )
257
258 absoluteTabIndex = (relativeIndex, gBrowser, {pinnedSeparate}) ->
259 tabs = gBrowser.visibleTabs
260 {selectedTab} = gBrowser
261
262 currentIndex = tabs.indexOf(selectedTab)
263 absoluteIndex = currentIndex + relativeIndex
264 numTabsTotal = tabs.length
265 numPinnedTabs = gBrowser._numPinnedTabs
266
267 [numTabs, min] = switch
268 when not pinnedSeparate
269 [numTabsTotal, 0]
270 when selectedTab.pinned
271 [numPinnedTabs, 0]
272 else
273 [numTabsTotal - numPinnedTabs, numPinnedTabs]
274
275 # Wrap _once_ if at one of the ends of the tab bar and cannot move in the
276 # current direction.
277 if (relativeIndex < 0 and currentIndex == min) or
278 (relativeIndex > 0 and currentIndex == min + numTabs - 1)
279 if absoluteIndex < min
280 absoluteIndex += numTabs
281 else if absoluteIndex >= min + numTabs
282 absoluteIndex -= numTabs
283
284 absoluteIndex = Math.max(min, absoluteIndex)
285 absoluteIndex = Math.min(absoluteIndex, min + numTabs - 1)
286
287 return absoluteIndex
288
289 helper_switch_tab = (direction, {vim, count = 1}) ->
290 {gBrowser} = vim.window
291 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: false})
292 utils.nextTick(vim.window, ->
293 gBrowser.selectTabAtIndex(index)
294 )
295
296 commands.tab_select_previous = helper_switch_tab.bind(null, -1)
297
298 commands.tab_select_next = helper_switch_tab.bind(null, +1)
299
300 helper_is_visited = (tab) ->
301 return tab.getAttribute('VimFx-visited') or not tab.getAttribute('unread')
302
303 commands.tab_select_most_recent = ({vim, count = 1}) ->
304 {gBrowser} = vim.window
305 tabsSorted =
306 Array.filter(
307 gBrowser.tabs,
308 (tab) -> not tab.closing and helper_is_visited(tab)
309 ).sort((a, b) -> b.lastAccessed - a.lastAccessed)[1..] # Remove current tab.
310 tab = tabsSorted[Math.min(count - 1, tabsSorted.length - 1)]
311 if tab
312 gBrowser.selectedTab = tab
313 else
314 vim.notify(translate('notification.tab_select_most_recent.none'))
315
316 commands.tab_select_oldest_unvisited = ({vim, count = 1}) ->
317 {gBrowser} = vim.window
318 tabsSorted =
319 Array.filter(
320 gBrowser.tabs,
321 (tab) -> not tab.closing and not helper_is_visited(tab)
322 ).sort((a, b) -> a.lastAccessed - b.lastAccessed)
323 tab = tabsSorted[Math.min(count - 1, tabsSorted.length - 1)]
324 if tab
325 gBrowser.selectedTab = tab
326 else
327 vim.notify(translate('notification.tab_select_oldest_unvisited.none'))
328
329 helper_move_tab = (direction, {vim, count = 1}) ->
330 {gBrowser} = vim.window
331 index = absoluteTabIndex(direction * count, gBrowser, {pinnedSeparate: true})
332 utils.nextTick(vim.window, ->
333 gBrowser.moveTabTo(gBrowser.selectedTab, index)
334 )
335
336 commands.tab_move_backward = helper_move_tab.bind(null, -1)
337
338 commands.tab_move_forward = helper_move_tab.bind(null, +1)
339
340 commands.tab_move_to_window = ({vim}) ->
341 {gBrowser} = vim.window
342 gBrowser.replaceTabWithWindow(gBrowser.selectedTab)
343
344 commands.tab_select_first = ({vim, count = 1}) ->
345 utils.nextTick(vim.window, ->
346 vim.window.gBrowser.selectTabAtIndex(count - 1)
347 )
348
349 commands.tab_select_first_non_pinned = ({vim, count = 1}) ->
350 firstNonPinned = vim.window.gBrowser._numPinnedTabs
351 utils.nextTick(vim.window, ->
352 vim.window.gBrowser.selectTabAtIndex(firstNonPinned + count - 1)
353 )
354
355 commands.tab_select_last = ({vim, count = 1}) ->
356 utils.nextTick(vim.window, ->
357 vim.window.gBrowser.selectTabAtIndex(-count)
358 )
359
360 commands.tab_toggle_pinned = ({vim}) ->
361 currentTab = vim.window.gBrowser.selectedTab
362 if currentTab.pinned
363 vim.window.gBrowser.unpinTab(currentTab)
364 else
365 vim.window.gBrowser.pinTab(currentTab)
366
367 commands.tab_close = ({vim, count = 1}) ->
368 {gBrowser} = vim.window
369 return if gBrowser.selectedTab.pinned
370 currentIndex = gBrowser.visibleTabs.indexOf(gBrowser.selectedTab)
371 utils.nextTick(vim.window, ->
372 for tab in gBrowser.visibleTabs[currentIndex...(currentIndex + count)]
373 gBrowser.removeTab(tab)
374 return
375 )
376
377 commands.tab_restore = ({vim, count = 1}) ->
378 utils.nextTick(vim.window, ->
379 for index in [0...count] by 1
380 restoredTab = vim.window.undoCloseTab()
381 if not restoredTab and index == 0
382 vim.notify(translate('notification.tab_restore.none'))
383 break
384 return
385 )
386
387 commands.tab_restore_list = ({vim}) ->
388 {window} = vim
389 fragment = window.RecentlyClosedTabsAndWindowsMenuUtils.getTabsFragment(
390 window, 'menuitem'
391 )
392 if fragment.childElementCount == 0
393 vim.notify(translate('notification.tab_restore.none'))
394 else
395 utils.openPopup(utils.injectTemporaryPopup(window.document, fragment))
396
397 commands.tab_close_to_end = ({vim}) ->
398 {gBrowser} = vim.window
399 gBrowser.removeTabsToTheEndFrom(gBrowser.selectedTab)
400
401 commands.tab_close_other = ({vim}) ->
402 {gBrowser} = vim.window
403 gBrowser.removeAllTabsBut(gBrowser.selectedTab)
404
405
406
407 helper_follow = (name, vim, callback, count = null) ->
408 vim.markPageInteraction()
409 help.removeHelp(vim.window)
410
411 # Enter hints mode immediately, with an empty set of markers. The user might
412 # press keys before the `vim._run` callback is invoked. Those key presses
413 # should be handled in hints mode, not normal mode.
414 initialMarkers = []
415 storage = vim.enterMode(
416 'hints', initialMarkers, callback, count, vim.options.hints_sleep
417 )
418
419 setMarkers = ({wrappers, viewport}) ->
420 if wrappers.length > 0
421 {markers, markerMap} = hints.injectHints(
422 vim.window, wrappers, viewport, vim.options
423 )
424 storage.markers = markers
425 storage.markerMap = markerMap
426 else
427 vim.notify(translate('notification.follow.none'))
428 vim.enterMode('normal')
429
430 vim._run(name, {}, (result) ->
431 # The user might have exited hints mode (and perhaps even entered it again)
432 # before this callback is invoked. If so, `storage.markers` has been
433 # cleared, or set to a new value. Only proceed if it is unchanged.
434 return unless storage.markers == initialMarkers
435 setMarkers(result)
436 storage.markEverything = ->
437 lastMarkers = storage.markers
438 vim._run(name, {markEverything: true}, (newResult) ->
439 return unless storage.markers == lastMarkers
440 setMarkers(newResult)
441 )
442 )
443
444 helper_follow_clickable = (options, {vim, count = 1}) ->
445 callback = (marker, timesLeft, keyStr) ->
446 {inTab, inBackground} = options
447 {type, elementIndex} = marker.wrapper
448 isLast = (timesLeft == 1)
449 isLink = (type == 'link')
450
451 switch
452 when keyStr.startsWith(vim.options.hints_toggle_in_tab)
453 inTab = not inTab
454 when keyStr.startsWith(vim.options.hints_toggle_in_background)
455 inTab = true
456 inBackground = not inBackground
457 else
458 unless isLast
459 inTab = true
460 inBackground = true
461
462 inTab = false unless isLink
463
464 if type == 'text' or (isLink and not (inTab and inBackground))
465 isLast = true
466
467 vim._focusMarkerElement(elementIndex)
468
469 if inTab
470 utils.nextTick(vim.window, ->
471 # `ContentClick.contentAreaClick` is what Firefox invokes when you click
472 # links using the mouse. Using that instead of simply
473 # `gBrowser.loadOneTab(url, options)` gives better interoperability with
474 # other add-ons, such as Tree Style Tab and BackTrack Tab History.
475 reset = prefs.root.tmp('browser.tabs.loadInBackground', true)
476 ContentClick.contentAreaClick({
477 href: marker.wrapper.href
478 shiftKey: not inBackground
479 ctrlKey: true
480 metaKey: true
481 }, vim.browser)
482 reset()
483 )
484 else
485 vim._run('click_marker_element', {
486 elementIndex, type
487 preventTargetBlank: vim.options.prevent_target_blank
488 })
489
490 return not isLast
491
492 name = if options.inTab then 'follow_in_tab' else 'follow'
493 helper_follow(name, vim, callback, count)
494
495 commands.follow =
496 helper_follow_clickable.bind(null, {inTab: false, inBackground: true})
497
498 commands.follow_in_tab =
499 helper_follow_clickable.bind(null, {inTab: true, inBackground: true})
500
501 commands.follow_in_focused_tab =
502 helper_follow_clickable.bind(null, {inTab: true, inBackground: false})
503
504 commands.follow_in_window = ({vim}) ->
505 callback = (marker) ->
506 vim._focusMarkerElement(marker.wrapper.elementIndex)
507 {href} = marker.wrapper
508 vim.window.openLinkIn(href, 'window', {}) if href
509 helper_follow('follow_in_tab', vim, callback)
510
511 commands.follow_multiple = (args) ->
512 args.count = Infinity
513 commands.follow(args)
514
515 commands.follow_copy = ({vim}) ->
516 callback = (marker) ->
517 {elementIndex} = marker.wrapper
518 property = switch marker.wrapper.type
519 when 'link'
520 'href'
521 when 'text'
522 'value'
523 when 'contenteditable', 'other'
524 'textContent'
525 vim._run('copy_marker_element', {elementIndex, property})
526 helper_follow('follow_copy', vim, callback)
527
528 commands.follow_focus = ({vim}) ->
529 callback = (marker) ->
530 vim._focusMarkerElement(marker.wrapper.elementIndex, {select: true})
531 helper_follow('follow_focus', vim, callback)
532
533 commands.click_browser_element = ({vim}) ->
534 markerElements = []
535
536 filter = ({markEverything}, element, getElementShape) ->
537 document = element.ownerDocument
538 semantic = true
539 type = switch
540 when vim._state.scrollableElements.has(element)
541 'scrollable'
542 when utils.isFocusable(element) or
543 (element.onclick and element.localName != 'statuspanel')
544 'clickable'
545
546 if markEverything and not type
547 type = 'other'
548 semantic = false
549
550 return unless type
551 return unless shape = getElementShape(element)
552 length = markerElements.push(element)
553 return {type, semantic, shape, elementIndex: length - 1}
554
555 callback = (marker) ->
556 element = markerElements[marker.wrapper.elementIndex]
557 switch marker.wrapper.type
558 when 'scrollable'
559 utils.focusElement(element, {flag: 'FLAG_BYKEY'})
560 when 'clickable', 'other'
561 sequence =
562 if element.localName == 'tab'
563 ['mousedown']
564 else
565 'click-xul'
566 utils.focusElement(element)
567 utils.simulateMouseEvents(element, sequence)
568
569 createMarkers = (wrappers) ->
570 viewport = utils.getWindowViewport(vim.window)
571 {markers} = hints.injectHints(vim.window, wrappers, viewport, {
572 hint_chars: vim.options.hint_chars
573 ui: true
574 })
575 return markers
576
577 wrappers = hints.getMarkableElements(
578 vim.window, filter.bind(null, {markEverything: false})
579 )
580
581 if wrappers.length > 0
582 storage = vim.enterMode('hints', createMarkers(wrappers), callback)
583 storage.markEverything = ->
584 newWrappers = hints.getMarkableElements(
585 vim.window, filter.bind(null, {markEverything: true})
586 )
587 storage.markers = createMarkers(newWrappers)
588 else
589 vim.notify(translate('notification.follow.none'))
590
591 helper_follow_pattern = (type, {vim}) ->
592 options = {
593 pattern_selector: vim.options.pattern_selector
594 pattern_attrs: vim.options.pattern_attrs
595 patterns: vim.options["#{type}_patterns"]
596 }
597 vim._run('follow_pattern', {type, options})
598
599 commands.follow_previous = helper_follow_pattern.bind(null, 'prev')
600
601 commands.follow_next = helper_follow_pattern.bind(null, 'next')
602
603 commands.focus_text_input = ({vim, count}) ->
604 vim.markPageInteraction()
605 vim._run('focus_text_input', {count})
606
607 helper_follow_selectable = ({select}, {vim}) ->
608 callback = (marker) ->
609 vim._run('element_text_select', {
610 elementIndex: marker.wrapper.elementIndex
611 full: select
612 })
613 vim.enterMode('caret', select)
614 helper_follow('follow_selectable', vim, callback)
615
616 commands.element_text_caret =
617 helper_follow_selectable.bind(null, {select: false})
618
619 commands.element_text_select =
620 helper_follow_selectable.bind(null, {select: true})
621
622 commands.element_text_copy = ({vim}) ->
623 callback = (marker) ->
624 vim._run('copy_marker_element', {
625 elementIndex: marker.wrapper.elementIndex
626 property: 'textContent'
627 })
628 helper_follow('follow_selectable', vim, callback)
629
630
631
632 findStorage = {lastSearchString: ''}
633
634 helper_find = ({highlight, linksOnly = false}, {vim}) ->
635 helpSearchInput = help.getSearchInput(vim.window)
636 if helpSearchInput
637 helpSearchInput.select()
638 return
639
640 helper_mark_last_scroll_position(vim)
641 findBar = vim.window.gBrowser.getFindBar()
642
643 mode = if linksOnly then findBar.FIND_LINKS else findBar.FIND_NORMAL
644 findBar.startFind(mode)
645 utils.focusElement(findBar._findField, {select: true})
646
647 return if linksOnly
648 return unless highlightButton = findBar.getElement('highlight')
649 if highlightButton.checked != highlight
650 highlightButton.click()
651
652 commands.find = helper_find.bind(null, {highlight: false})
653
654 commands.find_highlight_all = helper_find.bind(null, {highlight: true})
655
656 commands.find_links_only = helper_find.bind(null, {linksOnly: true})
657
658 helper_find_again = (direction, {vim}) ->
659 findBar = vim.window.gBrowser.getFindBar()
660 if findStorage.lastSearchString.length == 0
661 vim.notify(translate('notification.find_again.none'))
662 return
663 helper_mark_last_scroll_position(vim)
664 findBar._findField.value = findStorage.lastSearchString
665 findBar.onFindAgainCommand(direction)
666 message = findBar._findStatusDesc.textContent
667 vim.notify(message) if message
668
669 commands.find_next = helper_find_again.bind(null, false)
670
671 commands.find_previous = helper_find_again.bind(null, true)
672
673
674
675 commands.window_new = ({vim}) ->
676 vim.window.OpenBrowserWindow()
677
678 commands.window_new_private = ({vim}) ->
679 vim.window.OpenBrowserWindow({private: true})
680
681 commands.enter_mode_ignore = ({vim}) ->
682 vim.enterMode('ignore', {type: 'explicit'})
683
684 # Quote next keypress (pass it through to the page).
685 commands.quote = ({vim, count = 1}) ->
686 vim.enterMode('ignore', {type: 'explicit', count})
687
688 commands.enter_reader_view = ({vim}) ->
689 button = vim.window.document.getElementById('reader-mode-button')
690 if not button?.hidden
691 button.click()
692 else
693 vim.notify(translate('notification.enter_reader_view.none'))
694
695 commands.reload_config_file = ({vim}) ->
696 vim._parent.emit('shutdown')
697 config.load(vim._parent, (status) -> switch status
698 when null
699 vim.notify(translate('notification.reload_config_file.none'))
700 when true
701 vim.notify(translate('notification.reload_config_file.success'))
702 else
703 vim.notify(translate('notification.reload_config_file.failure'))
704 )
705
706 commands.help = ({vim}) ->
707 help.injectHelp(vim.window, vim._parent)
708
709 commands.dev = ({vim}) ->
710 vim.window.DeveloperToolbar.show(true) # `true` to focus.
711
712 commands.esc = ({vim}) ->
713 vim._run('esc')
714 utils.blurActiveBrowserElement(vim)
715 vim.window.DeveloperToolbar.hide()
716 vim.window.gBrowser.getFindBar().close()
717 hints.removeHints(vim.window) # Better safe than sorry.
718
719 unless help.getSearchInput(vim.window)?.getAttribute('focused')
720 help.removeHelp(vim.window)
721
722
723
724 module.exports = {
725 commands
726 findStorage
727 }
Imprint / Impressum