2 # Copyright Anton Khodakivskiy 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
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 # This file defines VimFx’s modes, and their respective commands. The Normal
23 # mode commands are defined in commands.coffee, though.
25 {commands, findStorage} = require('./commands')
26 defaults = require('./defaults')
27 help = require('./help')
28 hints = require('./hints')
29 translate = require('./l10n')
30 {rotateOverlappingMarkers} = require('./marker')
31 utils = require('./utils')
33 # Helper to create modes in a DRY way.
34 mode = (modeName, obj, commands = null) ->
35 obj.name = translate("mode.#{modeName}")
36 obj.order = defaults.mode_order[modeName]
38 for commandName, fn of commands
39 pref = "mode.#{modeName}.#{commandName}"
40 obj.commands[commandName] =
41 pref: defaults.BRANCH + pref
43 category: defaults.categoryMap[pref]
44 description: translate(pref)
45 order: defaults.command_order[pref]
46 exports[modeName] = obj
51 onEnter: ({vim, storage}, options = {}) ->
53 storage.returnTo = options.returnTo
54 else if storage.returnTo
55 vim.enterMode(storage.returnTo)
56 storage.returnTo = null
59 vim._run('clear_inputs')
61 onInput: (args, match) ->
62 {vim, storage, uiEvent} = args
65 autoInsertMode = (match.focus != null)
66 if match.type == 'none' or
67 (autoInsertMode and not match.specialKeys['<force>'])
70 vim.enterMode(storage.returnTo)
71 storage.returnTo = null
72 # If you press `aa` (and `a` is a prefix key, but there’s no `aa`
73 # shortcut), don’t pass the second `a` to the page.
74 return not match.toplevel
76 if match.type == 'full'
77 match.command.run(args)
79 # If the command changed the mode, wait until coming back from that mode
80 # before switching to `storage.returnTo` if any (see `onEnter` above).
81 if storage.returnTo and vim.mode == 'normal'
82 vim.enterMode(storage.returnTo)
83 storage.returnTo = null
85 # At this point the match is either full, partial or part of a count. Then
86 # we always want to suppress, except for one case: The Escape key.
87 return true unless keyStr == '<escape>'
89 # Passing Escape through allows for stopping the loading of the page and
90 # closing many custom dialogs (and perhaps other things; Escape is a very
93 # In browser UI the biggest reasons are allowing to reset the location bar
94 # when blurring it, and closing dialogs such as the “bookmark this page”
95 # dialog (<c-d>). However, an exception is made for the devtools (<c-K>).
96 # There, trying to unfocus the devtools using Escape would annoyingly
97 # open the split console.
98 return uiEvent.originalTarget.ownerGlobal.DevTools?
100 # In web pages content, an exception is made if we’re in autoInsertMode.
101 # That allows for blurring an input in a custom dialog without closing the
103 return autoInsertMode
105 # Note that this special handling of Escape is only used in Normal mode.
106 # There are two reasons we might suppress it in other modes. If some custom
107 # dialog of a website is open, we should be able to cancel hint markers on
108 # it without closing it. Secondly, otherwise cancelling hint markers on
109 # Google causes its search bar to be focused.
116 onEnter: ({vim, storage}, markers, callback, count = 1) ->
117 storage.markers = markers
118 storage.markerMap = null
119 storage.callback = callback
120 storage.count = count
121 storage.numEnteredChars = 0
123 storage.clearInterval = utils.setInterval(vim.window, 0, (next) ->
124 unless storage.markerMap
127 vim._send('getMarkableElementsMovements', null, (diffs) ->
128 for {dx, dy}, index in diffs when not (dx == 0 and dy == 0)
129 storage.markerMap[index].updatePosition(dx, dy)
134 # Expose the storage so asynchronously computed markers can be set
138 onLeave: ({vim, storage}) ->
139 vim.window.setTimeout(hints.removeHints.bind(null, vim.window),
140 vim.options.hints_timeout)
141 storage.clearInterval?()
146 onInput: (args, match) ->
147 {vim, storage} = args
148 {markers, callback} = storage
150 if match.type == 'full'
151 match.command.run(args)
152 else if match.unmodifiedKey in vim.options.hint_chars and markers.length > 0
155 for marker in markers when marker.hintIndex == storage.numEnteredChars
156 matched = marker.matchHintChar(match.unmodifiedKey)
157 marker.hide() unless matched
158 if marker.isMatched()
159 marker.markMatched(true)
160 matchedMarkers.push(marker)
162 if matchedMarkers.length > 0
163 again = callback(matchedMarkers[0], storage.count, match.keyStr)
166 vim.window.setTimeout((->
167 marker.markMatched(false) for marker in matchedMarkers
169 ), vim.options.hints_timeout)
170 marker.reset() for marker in markers
171 storage.numEnteredChars = 0
173 vim.enterMode('normal')
175 storage.numEnteredChars++
180 exit: ({vim, storage}) ->
181 # The hints are removed automatically when leaving the mode, but after a
182 # timeout. When aborting the mode we should remove the hints immediately.
183 hints.removeHints(vim.window)
184 vim.enterMode('normal')
186 rotate_markers_forward: ({storage}) ->
187 rotateOverlappingMarkers(storage.markers, true)
189 rotate_markers_backward: ({storage}) ->
190 rotateOverlappingMarkers(storage.markers, false)
192 delete_hint_char: ({storage}) ->
193 for marker in storage.markers
194 switch marker.hintIndex - storage.numEnteredChars
195 when 0 then marker.deleteHintChar()
196 when -1 then marker.show()
197 storage.numEnteredChars-- unless storage.numEnteredChars == 0
199 increase_count: ({storage}) -> storage.count++
205 onEnter: ({vim, storage}, count = null) ->
206 storage.count = count
208 onLeave: ({vim, storage}) ->
209 vim._run('blur_active_element') unless storage.count?
211 onInput: (args, match) ->
212 {vim, storage} = args
215 if match.type == 'full'
216 match.command.run(args)
219 vim.enterMode('normal')
225 exit: ({vim}) -> vim.enterMode('normal')
226 unquote: ({vim}) -> vim.enterMode('normal', {returnTo: 'ignore'})
235 findBar = vim.window.gBrowser.getFindBar()
236 findStorage.lastSearchString = findBar._findField.value
238 onInput: (args, match) ->
239 args.findBar = args.vim.window.gBrowser.getFindBar()
240 if match.type == 'full'
241 match.command.run(args)
246 exit: ({findBar}) -> findBar.close()
252 onEnter: ({storage}, callback) ->
253 storage.callback = callback
255 onLeave: ({storage}) ->
256 storage.callback = null
258 onInput: (args, match) ->
259 {vim, storage} = args
260 storage.callback(match.keyStr)
261 vim.enterMode('normal')