2 # Copyright Anton Khodakivskiy 2013, 2014.
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 { injectHints } = require('./hints')
24 { rotateOverlappingMarkers } = require('./marker')
25 { updateToolbarButton } = require('./button')
27 , searchForMatchingCommand
30 , findStorage } = require('./commands')
32 { interfaces: Ci } = Components
34 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 storage.keys.push(keyStr)
50 { match, exact, command, count } = searchForMatchingCommand(storage.keys)
52 if vim.blacklistedKeys and storage.keys.join('') in vim.blacklistedKeys
57 if autoInsertMode and command != escapeCommand
62 command.func(vim, event, count)
63 storage.keys.length = 0
65 # Esc key is not suppressed, and passed to the browser in normal mode.
67 # - It allows for stopping the loading of the page.
68 # - It allows for closing many custom dialogs (and perhaps other things
69 # -- Esc is a very commonly used key).
70 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
71 # an element. That allows for blurring an input in a custom dialog
72 # without closing the dialog too.
73 # - There are two reasons we might suppress it in other modes. If some
74 # custom dialog of a website is open, we should be able to cancel hint
75 # markers on it without closing it. Secondly, otherwise cancelling hint
76 # markers on Google causes its search bar to be focused.
77 # - It may only be suppressed in web pages, not in browser chrome. That
78 # allows for reseting the location bar when blurring it, and closing
79 # dialogs such as the “bookmark this page” dialog (<c-d>).
80 document = event.originalTarget.ownerDocument
81 inBrowserChrome = (document instanceof XULDocument)
82 if keyStr == '<escape>' and (not autoInsertMode or inBrowserChrome)
88 storage.keys.length = 0 unless /\d/.test(keyStr)
95 onEnter: (vim, storage, count = null) ->
97 updateToolbarButton(vim.rootWindow, {insertMode: true})
99 updateToolbarButton(vim.rootWindow, {insertMode: false})
100 utils.blurActiveElement(vim.window)
101 onInput: (vim, storage, keyStr) ->
104 if @commands['exit'].match(keyStr)
105 vim.enterMode('normal')
108 vim.enterMode('normal')
119 findBar = vim.rootWindow.gBrowser.getFindBar()
120 findStorage.lastSearchString = findBar._findField.value
122 onInput: (vim, storage, keyStr) ->
123 findBar = vim.rootWindow.gBrowser.getFindBar()
124 if @commands['exit'].match(keyStr)
130 exit: ['<escape>', '<enter>']
133 onEnter: (vim, storage, filter, callback) ->
134 [ markers, container ] = injectHints(vim.rootWindow, vim.window, filter)
135 if markers.length > 0
136 storage.markers = markers
137 storage.container = container
138 storage.callback = callback
139 storage.numEnteredChars = 0
141 vim.enterMode('normal')
143 onLeave: (vim, storage) ->
144 { container } = storage
145 vim.rootWindow.setTimeout((->
151 onInput: (vim, storage, keyStr, event) ->
152 { markers, callback } = storage
155 when @commands['exit'].match(keyStr)
156 # Remove the hints immediately.
157 storage.container?.remove()
158 vim.enterMode('normal')
161 when @commands['rotate_markers_forward'].match(keyStr)
162 rotateOverlappingMarkers(markers, true)
163 when @commands['rotate_markers_backward'].match(keyStr)
164 rotateOverlappingMarkers(markers, false)
166 when @commands['delete_hint_char'].match(keyStr)
167 for marker in markers
168 switch marker.hintIndex - storage.numEnteredChars
169 when 0 then marker.deleteHintChar()
170 when -1 then marker.show()
171 storage.numEnteredChars-- unless storage.numEnteredChars == 0
174 if keyStr not in utils.getHintChars()
177 for marker in markers when marker.hintIndex == storage.numEnteredChars
178 match = marker.matchHintChar(keyStr)
179 marker.hide() unless match
180 if marker.isMatched()
181 marker.markMatched(true)
182 matchedMarker = marker
184 again = callback(matchedMarker)
186 vim.rootWindow.setTimeout((->
187 matchedMarker.markMatched(false)
189 marker.reset() for marker in markers
190 storage.numEnteredChars = 0
192 vim.enterMode('normal')
194 storage.numEnteredChars++
202 rotate_markers_forward: ['<space>']
203 rotate_markers_backward: ['<s-space>']
204 delete_hint_char: ['<backspace>']
206 for modeName of exports
207 mode = exports[modeName]
208 continue if Array.isArray(mode.commands)
209 for commandName of mode.commands
210 name = "mode_#{ modeName }_#{ commandName }"
211 keys = mode.commands[commandName].map((key) -> [key])
212 mode.commands[commandName] = new Command(null, name, null, keys)