]> git.gir.st - VimFx.git/blob - extension/lib/modes.coffee
Fix misplaced hint markers when zoomed
[VimFx.git] / extension / lib / modes.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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, 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')
32
33 # Helper to create modes in a DRY way.
34 mode = (modeName, obj, commands = null) ->
35 obj.name = translate.bind(null, "mode.#{modeName}")
36 obj.order = defaults.mode_order[modeName]
37 obj.commands = {}
38 for commandName, fn of commands
39 pref = "mode.#{modeName}.#{commandName}"
40 obj.commands[commandName] =
41 pref: defaults.BRANCH + pref
42 run: fn
43 category: defaults.categoryMap[pref]
44 description: translate.bind(null, pref)
45 order: defaults.command_order[pref]
46 exports[modeName] = obj
47
48
49
50 mode('normal', {
51 onEnter: ({vim, storage}, options = {}) ->
52 if options.returnTo
53 storage.returnTo = options.returnTo
54 else if storage.returnTo
55 vim.enterMode(storage.returnTo)
56 storage.returnTo = null
57
58 onLeave: ({vim}) ->
59 vim._run('clear_inputs')
60 help.removeHelp(vim.window)
61
62 onInput: (args, match) ->
63 {vim, storage, uiEvent} = args
64 {keyStr} = match
65
66 autoInsertMode = (match.focus != null)
67 if match.type == 'none' or
68 (autoInsertMode and not match.specialKeys['<force>'])
69 match.discard()
70 if storage.returnTo
71 vim.enterMode(storage.returnTo)
72 storage.returnTo = null
73 # If you press `aa` (and `a` is a prefix key, but there’s no `aa`
74 # shortcut), don’t pass the second `a` to the page.
75 return not match.toplevel
76
77 if match.type == 'full'
78 match.command.run(args)
79
80 # If the command changed the mode, wait until coming back from that mode
81 # before switching to `storage.returnTo` if any (see `onEnter` above).
82 if storage.returnTo and vim.mode == 'normal'
83 vim.enterMode(storage.returnTo)
84 storage.returnTo = null
85
86 # At this point the match is either full, partial or part of a count. Then
87 # we always want to suppress, except for one case: The Escape key.
88 return true unless keyStr == '<escape>'
89
90 # Passing Escape through allows for stopping the loading of the page and
91 # closing many custom dialogs (and perhaps other things; Escape is a very
92 # commonly used key).
93 if uiEvent
94 # In browser UI the biggest reasons are allowing to reset the location bar
95 # when blurring it, and closing dialogs such as the “bookmark this page”
96 # dialog (<c-d>). However, an exception is made for the devtools (<c-K>).
97 # There, trying to unfocus the devtools using Escape would annoyingly
98 # open the split console.
99 return uiEvent.originalTarget.ownerGlobal.DevTools?
100 else
101 # In web pages content, an exception is made if we’re in autoInsertMode.
102 # That allows for blurring an input in a custom dialog without closing the
103 # dialog too.
104 return autoInsertMode
105
106 # Note that this special handling of Escape is only used in Normal mode.
107 # There are two reasons we might suppress it in other modes. If some custom
108 # dialog of a website is open, we should be able to cancel hint markers on
109 # it without closing it. Secondly, otherwise cancelling hint markers on
110 # Google causes its search bar to be focused.
111
112 }, commands)
113
114
115
116 mode('hints', {
117 onEnter: ({vim, storage}, markers, callback, count = 1) ->
118 storage.markers = markers
119 storage.callback = callback
120 storage.count = count
121 storage.numEnteredChars = 0
122
123 # Expose the storage so asynchronously computed markers can be set
124 # retroactively.
125 return storage
126
127 onLeave: ({vim, storage}) ->
128 vim.window.setTimeout(hints.removeHints.bind(null, vim.window),
129 vim.options.hints_timeout)
130 for key of storage
131 storage[key] = null
132 return
133
134 onInput: (args, match) ->
135 {vim, storage} = args
136 {markers, callback} = storage
137
138 if match.type == 'full'
139 match.command.run(args)
140 else if match.unmodifiedKey in vim.options.hint_chars and markers.length > 0
141 matchedMarkers = []
142
143 for marker in markers when marker.hintIndex == storage.numEnteredChars
144 matched = marker.matchHintChar(match.unmodifiedKey)
145 marker.hide() unless matched
146 if marker.isMatched()
147 marker.markMatched(true)
148 matchedMarkers.push(marker)
149
150 if matchedMarkers.length > 0
151 again = callback(matchedMarkers[0], storage.count, match.keyStr)
152 storage.count--
153 if again
154 vim.window.setTimeout((->
155 marker.markMatched(false) for marker in matchedMarkers
156 return
157 ), vim.options.hints_timeout)
158 marker.reset() for marker in markers
159 storage.numEnteredChars = 0
160 else
161 vim.enterMode('normal')
162 else
163 storage.numEnteredChars++
164
165 return true
166
167 }, {
168 exit: ({vim, storage}) ->
169 # The hints are removed automatically when leaving the mode, but after a
170 # timeout. When aborting the mode we should remove the hints immediately.
171 hints.removeHints(vim.window)
172 vim.enterMode('normal')
173
174 rotate_markers_forward: ({storage}) ->
175 rotateOverlappingMarkers(storage.markers, true)
176
177 rotate_markers_backward: ({storage}) ->
178 rotateOverlappingMarkers(storage.markers, false)
179
180 delete_hint_char: ({storage}) ->
181 for marker in storage.markers
182 switch marker.hintIndex - storage.numEnteredChars
183 when 0 then marker.deleteHintChar()
184 when -1 then marker.show()
185 storage.numEnteredChars-- unless storage.numEnteredChars == 0
186
187 increase_count: ({storage}) -> storage.count++
188 })
189
190
191
192 mode('ignore', {
193 onEnter: ({vim, storage}, count = null) ->
194 storage.count = count
195
196 onLeave: ({vim, storage}) ->
197 vim._run('blur_active_element') unless storage.count?
198
199 onInput: (args, match) ->
200 {vim, storage} = args
201 switch storage.count
202 when null
203 if match.type == 'full'
204 match.command.run(args)
205 return true
206 when 1
207 vim.enterMode('normal')
208 else
209 storage.count--
210 return false
211
212 }, {
213 exit: ({vim}) -> vim.enterMode('normal')
214 unquote: ({vim}) -> vim.enterMode('normal', {returnTo: 'ignore'})
215 })
216
217
218
219 mode('find', {
220 onEnter: ->
221
222 onLeave: ({vim}) ->
223 findBar = vim.window.gBrowser.getFindBar()
224 findStorage.lastSearchString = findBar._findField.value
225
226 onInput: (args, match) ->
227 args.findBar = args.vim.window.gBrowser.getFindBar()
228 if match.type == 'full'
229 match.command.run(args)
230 return true
231 return false
232
233 }, {
234 exit: ({findBar}) -> findBar.close()
235 })
236
237
238
239 mode('marks', {
240 onEnter: ({storage}, callback) ->
241 storage.callback = callback
242
243 onLeave: ({storage}) ->
244 storage.callback = null
245
246 onInput: (args, match) ->
247 {vim, storage} = args
248 storage.callback(match.keyStr)
249 vim.enterMode('normal')
250 return true
251 })
Imprint / Impressum