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 # This file defines VimFx’s modes, and their respective commands. The Normal
23 # mode commands are defined in commands.coffee, though.
26 , findStorage } = require('./commands')
27 defaults = require('./defaults')
28 help = require('./help')
29 hints = require('./hints')
30 translate = require('./l10n')
31 { rotateOverlappingMarkers } = require('./marker')
32 utils = require('./utils')
34 # Helper to create modes in a DRY way.
35 mode = (modeName, obj, commands) ->
36 obj.name = translate.bind(null, "mode.#{ modeName }")
37 obj.order = defaults.mode_order[modeName]
39 for commandName, fn of commands
40 pref = "mode.#{ modeName }.#{ commandName }"
41 obj.commands[commandName] =
42 pref: defaults.BRANCH + pref
44 category: defaults.categoryMap[pref]
45 description: translate.bind(null, pref)
46 order: defaults.command_order[pref]
47 exports[modeName] = obj
52 onEnter: ({ vim, storage }, options = {}) ->
54 storage.returnTo = options.returnTo
55 else if storage.returnTo
56 vim.enterMode(storage.returnTo)
57 storage.returnTo = null
60 vim._run('clear_inputs')
61 help.removeHelp(vim.window)
63 onInput: (args, match) ->
64 { vim, storage, isFrameEvent } = args
67 autoInsertMode = (match.focus != null)
68 if match.type == 'none' or
69 (autoInsertMode and not match.specialKeys['<force>'])
71 vim.enterMode(storage.returnTo)
72 storage.returnTo = null
75 if match.type == 'full'
76 match.command.run(args)
78 # If the command changed the mode, wait until coming back from that mode
79 # before switching to `storage.returnTo` if any (see `onEnter` above).
80 if storage.returnTo and vim.mode == 'normal'
81 vim.enterMode(storage.returnTo)
82 storage.returnTo = null
84 # At this point the match is either full, partial or part of a count. Then
85 # we always want to suppress, except for one case: The Escape key.
87 # - It allows for stopping the loading of the page.
88 # - It allows for closing many custom dialogs (and perhaps other things
89 # -- Esc is a very commonly used key).
90 # - It is not passed if Esc is used for `command.esc` and we’re blurring
91 # an element. That allows for blurring an input in a custom dialog
92 # without closing the dialog too.
93 # - There are two reasons we might suppress it in other modes. If some
94 # custom dialog of a website is open, we should be able to cancel hint
95 # markers on it without closing it. Secondly, otherwise cancelling hint
96 # markers on Google causes its search bar to be focused.
97 # - It may only be suppressed in web pages, not in browser chrome. That
98 # allows for resetting the location bar when blurring it, and closing
99 # dialogs such as the “bookmark this page” dialog (<c-d>).
100 return not (keyStr == '<escape>' and not (isFrameEvent and autoInsertMode))
107 onEnter: ({ vim, storage }, wrappers, viewport, callback, count = 1) ->
108 [ markers, container ] = hints.injectHints(
109 vim.window, wrappers, viewport, vim.options
111 if markers.length > 0
112 storage.markers = markers
113 storage.container = container
114 storage.callback = callback
115 storage.count = count
116 storage.numEnteredChars = 0
118 vim.enterMode('normal')
120 onLeave: ({ vim, storage }) ->
121 { container } = storage
122 vim.window.setTimeout((->
124 ), vim.options.hints_timeout)
128 onInput: (args, match) ->
129 { vim, storage } = args
130 { markers, callback } = storage
132 if match.type == 'full'
133 match.command.run(args)
134 else if match.unmodifiedKey in vim.options.hint_chars
137 for marker in markers when marker.hintIndex == storage.numEnteredChars
138 matched = marker.matchHintChar(match.unmodifiedKey)
139 marker.hide() unless matched
140 if marker.isMatched()
141 marker.markMatched(true)
142 matchedMarkers.push(marker)
144 if matchedMarkers.length > 0
145 again = callback(matchedMarkers[0], storage.count, match.keyStr)
148 vim.window.setTimeout((->
149 marker.markMatched(false) for marker in matchedMarkers
150 ), vim.options.hints_timeout)
151 marker.reset() for marker in markers
152 storage.numEnteredChars = 0
154 vim.enterMode('normal')
156 storage.numEnteredChars++
161 exit: ({ vim, storage }) ->
162 # The hints are removed automatically when leaving the mode, but after a
163 # timeout. When aborting the mode we should remove the hints immediately.
164 storage.container?.remove()
165 vim.enterMode('normal')
167 rotate_markers_forward: ({ storage }) ->
168 rotateOverlappingMarkers(storage.markers, true)
170 rotate_markers_backward: ({ storage }) ->
171 rotateOverlappingMarkers(storage.markers, false)
173 delete_hint_char: ({ storage }) ->
174 for marker in storage.markers
175 switch marker.hintIndex - storage.numEnteredChars
176 when 0 then marker.deleteHintChar()
177 when -1 then marker.show()
178 storage.numEnteredChars-- unless storage.numEnteredChars == 0
180 increase_count: ({ storage }) -> storage.count++
186 onEnter: ({ vim, storage }, count = null) ->
187 storage.count = count
189 onLeave: ({ vim, storage }) ->
190 vim._run('blur_active_element') unless storage.count?
192 onInput: (args, match) ->
193 { vim, storage } = args
196 if match.type == 'full'
197 match.command.run(args)
200 vim.enterMode('normal')
206 exit: ({ vim }) -> vim.enterMode('normal')
207 unquote: ({ vim }) -> vim.enterMode('normal', {returnTo: 'ignore'})
215 onLeave: ({ vim }) ->
216 findBar = vim.window.gBrowser.getFindBar()
217 findStorage.lastSearchString = findBar._findField.value
219 onInput: (args, match) ->
220 args.findBar = args.vim.window.gBrowser.getFindBar()
221 if match.type == 'full'
222 match.command.run(args)
227 exit: ({ findBar }) -> findBar.close()