1 utils = require 'utils'
2 keyUtils = require 'key-utils'
3 { Vim } = require 'vim'
4 { getPref } = require 'prefs'
5 { updateToolbarButton } = require 'button'
6 { unload } = require 'unload'
7 { commands } = require 'commands'
8 { modes } = require 'modes'
10 { interfaces: Ci } = Components
12 # Not suppressing Esc allows for stopping the loading of the page as well as closing many custom
13 # dialogs (and perhaps other things -- Esc is a very commonly used key). There are two reasons we
14 # might suppress it in other modes. If some custom dialog of a website is open, we should be able to
15 # cancel hint markers on it without closing it. Secondly, otherwise cancelling hint markers on
16 # google causes its search bar to be focused.
17 NEVER_SUPPRESS_IN_NORMAL_MODE = ['Esc']
19 # TODO: Should 'Esc' be configurable?
20 newFunc = (window) -> new Vim({window, commands, modes, esc: 'Esc'})
21 vimBucket = new utils.Bucket(utils.getWindowId, newFunc)
23 keyStrFromEvent = (event) ->
24 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
27 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
28 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
33 # Passthrough mode is activated when VimFx should temporarily stop processing keyboard input, for
34 # example when a menu is shown.
36 checkPassthrough = (event) ->
37 if event.target.nodeName in ['menupopup', 'panel']
38 passthrough = switch event.type
39 when 'popupshown' then true
40 when 'popuphidden' then false
43 keyListener = (event) ->
45 return if passthrough or getPref('disabled')
46 return unless window = utils.getEventCurrentTabWindow(event)
47 return unless vim = vimBucket.get(window)
48 return if vim.blacklisted
50 if event.type == 'keydown'
53 return unless keyStr = keyStrFromEvent(event)
55 isEditable = utils.isElementEditable(event.originalTarget)
56 unless isEditable and keyStr != vim.esc
58 # This check must be done before `vim.onInput()` below, since that call might change the
59 # mode. We are interested in the mode at the beginning of the events, not whatever it might
61 suppressException = (vim.mode == Vim.MODE_NORMAL and keyStr in NEVER_SUPPRESS_IN_NORMAL_MODE)
63 suppress = vim.onInput(keyStr, event)
68 event.preventDefault()
69 event.stopPropagation()
72 console.log("#{ error } (in #{ event.type })\n#{ error.stack.replace(/@.+-> /g, '@') }")
74 removeVimFromTab = (tab, gBrowser) ->
75 return unless browser = gBrowser.getBrowserForTab(tab)
76 vimBucket.forget(browser.contentWindow)
78 # The following listeners are installed on every top level Chrome window
83 popupshown: checkPassthrough
84 popuphidden: checkPassthrough
86 # When the top level window closes we should release all Vims that were
87 # associated with tabs in this window
88 DOMWindowClose: (event) ->
89 return unless { gBrowser } = event.originalTarget
90 for tab in gBrowser.tabs
91 removeVimFromTab(tab, gBrowser)
94 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
95 tab = event.originalTarget
96 removeVimFromTab(tab, gBrowser)
98 # Update the toolbar button icon to reflect the blacklisted state
100 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
101 return unless vim = vimBucket.get(window)
102 return unless rootWindow = utils.getRootWindow(window)
103 updateToolbarButton(rootWindow, {blacklisted: vim.blacklisted})
105 # This listener works on individual tabs within Chrome Window
107 # Listenfor location changes and disable the extension on blacklisted urls
108 onLocationChange: (browser, webProgress, request, location) ->
109 return unless vim = vimBucket.get(browser.contentWindow)
111 # If the location changes when in hints mode (for example because the reload button has been
112 # clicked), we're going to end up in hints mode without any markers. So switch back to normal
114 if vim.mode == 'hints'
115 vim.enterNormalMode()
117 return unless rootWindow = utils.getRootWindow(vim.window)
118 vim.blacklisted = utils.isBlacklisted(location.spec)
119 updateToolbarButton(rootWindow, {blacklisted: vim.blacklisted})
121 addEventListeners = (window) ->
122 for name, listener of windowsListeners
123 window.addEventListener(name, listener, true)
125 window.gBrowser.addTabsProgressListener(tabsListener)
128 for name, listener of windowsListeners
129 window.removeEventListener(name, listener, true)
131 window.gBrowser.removeTabsProgressListener(tabsListener)
133 exports.addEventListeners = addEventListeners