1 utils = require 'utils'
2 { mode_hints } = require 'mode-hints/mode-hints'
3 { updateToolbarButton } = require 'button'
4 { searchForMatchingCommand
7 , findStorage } = require 'commands'
9 { interfaces: Ci } = Components
11 XULDocument = Ci.nsIDOMXULDocument
16 onEnter: (vim, storage) ->
18 storage.commands ?= {}
20 onLeave: (vim, storage) ->
21 storage.keys.length = 0
23 onInput: (vim, storage, keyStr, event) ->
24 isEditable = utils.isElementEditable(event.originalTarget)
25 autoInsertMode = isEditable or vim.rootWindow.TabView.isVisible()
27 if autoInsertMode and not isEscCommandKey(keyStr)
30 storage.keys.push(keyStr)
32 { match, exact, command } = searchForMatchingCommand(storage.keys)
36 command.func(vim, event)
37 storage.keys.length = 0
39 # Esc key is not suppressed, and passed to the browser in normal mode.
41 # - It allows for stopping the loading of the page.
42 # - It allows for closing many custom dialogs (and perhaps other things
43 # -- Esc is a very commonly used key).
44 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
45 # an element. That allows for blurring an input in a custom dialog
46 # without closing the dialog too.
47 # - There are two reasons we might suppress it in other modes. If some
48 # custom dialog of a website is open, we should be able to cancel hint
49 # markers on it without closing it. Secondly, otherwise cancelling hint
50 # markers on Google causes its search bar to be focused.
51 # - It may only be suppressed in web pages, not in browser chrome. That
52 # allows for reseting the location bar when blurring it, and closing
53 # dialogs such as the “bookmark this page” dialog (<c-d>).
54 document = event.originalTarget.ownerDocument
55 inBrowserChrome = (document instanceof XULDocument)
56 if keyStr == 'Esc' and (not autoInsertMode or inBrowserChrome)
62 storage.keys.length = 0
67 updateToolbarButton(vim.rootWindow, {insertMode: true})
69 updateToolbarButton(vim.rootWindow, {insertMode: false})
70 utils.blurActiveElement(vim.window)
71 onInput: (vim, storage, keyStr) ->
72 if isEscCommandKey(keyStr)
73 vim.enterMode('normal')
80 findBar = vim.rootWindow.gBrowser.getFindBar()
81 findStorage.lastSearchString = findBar._findField.value
83 onInput: (vim, storage, keyStr) ->
84 findBar = vim.rootWindow.gBrowser.getFindBar()
85 if isEscCommandKey(keyStr) or keyStr == 'Return'
90 modes['hints'] = mode_hints