2 # Copyright Anton Khodakivskiy 2013.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2014.
6 # This file is part of VimFx.
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.
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.
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/>.
22 utils = require 'utils'
23 { mode_hints } = require 'mode-hints/mode-hints'
24 { updateToolbarButton } = require 'button'
25 { searchForMatchingCommand
28 , findStorage } = require 'commands'
30 { interfaces: Ci } = Components
32 XULDocument = Ci.nsIDOMXULDocument
37 onEnter: (vim, storage) ->
39 storage.commands ?= {}
41 onLeave: (vim, storage) ->
42 storage.keys.length = 0
44 onInput: (vim, storage, keyStr, event) ->
45 isEditable = utils.isElementEditable(event.originalTarget)
46 autoInsertMode = isEditable or vim.rootWindow.TabView.isVisible()
48 if autoInsertMode and not isEscCommandKey(keyStr)
51 storage.keys.push(keyStr)
53 { match, exact, command, count } = searchForMatchingCommand(storage.keys)
57 command.func(vim, event, count)
58 storage.keys.length = 0
60 # Esc key is not suppressed, and passed to the browser in normal mode.
62 # - It allows for stopping the loading of the page.
63 # - It allows for closing many custom dialogs (and perhaps other things
64 # -- Esc is a very commonly used key).
65 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
66 # an element. That allows for blurring an input in a custom dialog
67 # without closing the dialog too.
68 # - There are two reasons we might suppress it in other modes. If some
69 # custom dialog of a website is open, we should be able to cancel hint
70 # markers on it without closing it. Secondly, otherwise cancelling hint
71 # markers on Google causes its search bar to be focused.
72 # - It may only be suppressed in web pages, not in browser chrome. That
73 # allows for reseting the location bar when blurring it, and closing
74 # dialogs such as the “bookmark this page” dialog (<c-d>).
75 document = event.originalTarget.ownerDocument
76 inBrowserChrome = (document instanceof XULDocument)
77 if keyStr == 'Esc' and (not autoInsertMode or inBrowserChrome)
83 storage.keys.length = 0 unless /\d/.test(keyStr)
89 updateToolbarButton(vim.rootWindow, {insertMode: true})
91 updateToolbarButton(vim.rootWindow, {insertMode: false})
92 utils.blurActiveElement(vim.window)
93 onInput: (vim, storage, keyStr) ->
94 if isEscCommandKey(keyStr)
95 vim.enterMode('normal')
102 findBar = vim.rootWindow.gBrowser.getFindBar()
103 findStorage.lastSearchString = findBar._findField.value
105 onInput: (vim, storage, keyStr) ->
106 findBar = vim.rootWindow.gBrowser.getFindBar()
107 if isEscCommandKey(keyStr) or keyStr == 'Return'
112 modes['hints'] = mode_hints
114 exports.modes = modes