]> git.gir.st - VimFx.git/blob - extension/lib/modes.coffee
Make hints mode more robust
[VimFx.git] / extension / lib / modes.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 # Copyright Wang Zhuochun 2014.
5 #
6 # This file is part of VimFx.
7 #
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.
12 #
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.
17 #
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/>.
20 ###
21
22 # This file defines VimFx’s modes, and their respective commands. The Normal
23 # mode commands are defined in commands.coffee, though.
24
25 { commands
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')
33
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]
38 obj.commands = {}
39 for commandName, fn of commands
40 pref = "mode.#{ modeName }.#{ commandName }"
41 obj.commands[commandName] =
42 pref: defaults.BRANCH + pref
43 run: fn
44 category: defaults.categoryMap[pref]
45 description: translate.bind(null, pref)
46 order: defaults.command_order[pref]
47 exports[modeName] = obj
48
49
50
51 mode('normal', {
52 onEnter: ({ vim, storage }, options = {}) ->
53 if options.returnTo
54 storage.returnTo = options.returnTo
55 else if storage.returnTo
56 vim.enterMode(storage.returnTo)
57 storage.returnTo = null
58
59 onLeave: ({ vim }) ->
60 vim._run('clear_inputs')
61 help.removeHelp(vim.window)
62
63 onInput: (args, match) ->
64 { vim, storage, isFrameEvent } = args
65 { keyStr } = match
66
67 autoInsertMode = (match.focus != null)
68 if match.type == 'none' or
69 (autoInsertMode and not match.specialKeys['<force>'])
70 if storage.returnTo
71 vim.enterMode(storage.returnTo)
72 storage.returnTo = null
73 return false
74
75 if match.type == 'full'
76 match.command.run(args)
77
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
83
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.
86 #
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))
101
102 }, commands)
103
104
105
106 mode('hints', {
107 onEnter: ({ vim, storage }, markers, callback, count = 1) ->
108 storage.markers = markers
109 storage.callback = callback
110 storage.count = count
111 storage.numEnteredChars = 0
112
113 # Expose the storage so asynchronously computed markers can be set
114 # retroactively.
115 return storage
116
117 onLeave: ({ vim, storage }) ->
118 vim.window.setTimeout(hints.removeHints.bind(null, vim.window),
119 vim.options.hints_timeout)
120 for key of storage
121 storage[key] = null
122 return
123
124 onInput: (args, match) ->
125 { vim, storage } = args
126 { markers, callback } = storage
127
128 if match.type == 'full'
129 match.command.run(args)
130 else if match.unmodifiedKey in vim.options.hint_chars and markers.length > 0
131 matchedMarkers = []
132
133 for marker in markers when marker.hintIndex == storage.numEnteredChars
134 matched = marker.matchHintChar(match.unmodifiedKey)
135 marker.hide() unless matched
136 if marker.isMatched()
137 marker.markMatched(true)
138 matchedMarkers.push(marker)
139
140 if matchedMarkers.length > 0
141 again = callback(matchedMarkers[0], storage.count, match.keyStr)
142 storage.count--
143 if again
144 vim.window.setTimeout((->
145 marker.markMatched(false) for marker in matchedMarkers
146 ), vim.options.hints_timeout)
147 marker.reset() for marker in markers
148 storage.numEnteredChars = 0
149 else
150 vim.enterMode('normal')
151 else
152 storage.numEnteredChars++
153
154 return true
155
156 }, {
157 exit: ({ vim, storage }) ->
158 # The hints are removed automatically when leaving the mode, but after a
159 # timeout. When aborting the mode we should remove the hints immediately.
160 hints.removeHints(vim.window)
161 vim.enterMode('normal')
162
163 rotate_markers_forward: ({ storage }) ->
164 rotateOverlappingMarkers(storage.markers, true)
165
166 rotate_markers_backward: ({ storage }) ->
167 rotateOverlappingMarkers(storage.markers, false)
168
169 delete_hint_char: ({ storage }) ->
170 for marker in storage.markers
171 switch marker.hintIndex - storage.numEnteredChars
172 when 0 then marker.deleteHintChar()
173 when -1 then marker.show()
174 storage.numEnteredChars-- unless storage.numEnteredChars == 0
175
176 increase_count: ({ storage }) -> storage.count++
177 })
178
179
180
181 mode('ignore', {
182 onEnter: ({ vim, storage }, count = null) ->
183 storage.count = count
184
185 onLeave: ({ vim, storage }) ->
186 vim._run('blur_active_element') unless storage.count?
187
188 onInput: (args, match) ->
189 { vim, storage } = args
190 switch storage.count
191 when null
192 if match.type == 'full'
193 match.command.run(args)
194 return true
195 when 1
196 vim.enterMode('normal')
197 else
198 storage.count--
199 return false
200
201 }, {
202 exit: ({ vim }) -> vim.enterMode('normal')
203 unquote: ({ vim }) -> vim.enterMode('normal', {returnTo: 'ignore'})
204 })
205
206
207
208 mode('find', {
209 onEnter: ->
210
211 onLeave: ({ vim }) ->
212 findBar = vim.window.gBrowser.getFindBar()
213 findStorage.lastSearchString = findBar._findField.value
214
215 onInput: (args, match) ->
216 args.findBar = args.vim.window.gBrowser.getFindBar()
217 if match.type == 'full'
218 match.command.run(args)
219 return true
220 return false
221
222 }, {
223 exit: ({ findBar }) -> findBar.close()
224 })
Imprint / Impressum