]> git.gir.st - VimFx.git/blob - extension/lib/modes.coffee
Merge branch 'master' into develop
[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 dev tools (<c-K>).
97 # There, trying to unfocus the dev tools 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.markerMap = null
120 storage.callback = callback
121 storage.count = count
122 storage.numEnteredChars = 0
123
124 storage.clearInterval = utils.setInterval(vim.window, 0, (callback) ->
125 unless storage.markerMap
126 callback()
127 return
128 vim._send('getMarkableElementsMovements', null, (diffs) ->
129 for {dx, dy}, index in diffs when not (dx == 0 and dy == 0)
130 storage.markerMap[index].updatePosition(dx, dy)
131 callback()
132 )
133 )
134
135 # Expose the storage so asynchronously computed markers can be set
136 # retroactively.
137 return storage
138
139 onLeave: ({vim, storage}) ->
140 vim.window.setTimeout(hints.removeHints.bind(null, vim.window),
141 vim.options.hints_timeout)
142 storage.clearInterval?()
143 for key of storage
144 storage[key] = null
145 return
146
147 onInput: (args, match) ->
148 {vim, storage} = args
149 {markers, callback} = storage
150
151 if match.type == 'full'
152 match.command.run(args)
153 else if match.unmodifiedKey in vim.options.hint_chars and markers.length > 0
154 matchedMarkers = []
155
156 for marker in markers when marker.hintIndex == storage.numEnteredChars
157 matched = marker.matchHintChar(match.unmodifiedKey)
158 marker.hide() unless matched
159 if marker.isMatched()
160 marker.markMatched(true)
161 matchedMarkers.push(marker)
162
163 if matchedMarkers.length > 0
164 again = callback(matchedMarkers[0], storage.count, match.keyStr)
165 storage.count--
166 if again
167 vim.window.setTimeout((->
168 marker.markMatched(false) for marker in matchedMarkers
169 return
170 ), vim.options.hints_timeout)
171 marker.reset() for marker in markers
172 storage.numEnteredChars = 0
173 else
174 vim.enterMode('normal')
175 else
176 storage.numEnteredChars++
177
178 return true
179
180 }, {
181 exit: ({vim, storage}) ->
182 # The hints are removed automatically when leaving the mode, but after a
183 # timeout. When aborting the mode we should remove the hints immediately.
184 hints.removeHints(vim.window)
185 vim.enterMode('normal')
186
187 rotate_markers_forward: ({storage}) ->
188 rotateOverlappingMarkers(storage.markers, true)
189
190 rotate_markers_backward: ({storage}) ->
191 rotateOverlappingMarkers(storage.markers, false)
192
193 delete_hint_char: ({storage}) ->
194 for marker in storage.markers
195 switch marker.hintIndex - storage.numEnteredChars
196 when 0 then marker.deleteHintChar()
197 when -1 then marker.show()
198 storage.numEnteredChars-- unless storage.numEnteredChars == 0
199
200 increase_count: ({storage}) -> storage.count++
201 })
202
203
204
205 mode('ignore', {
206 onEnter: ({vim, storage}, count = null) ->
207 storage.count = count
208
209 onLeave: ({vim, storage}) ->
210 vim._run('blur_active_element') unless storage.count?
211
212 onInput: (args, match) ->
213 {vim, storage} = args
214 switch storage.count
215 when null
216 if match.type == 'full'
217 match.command.run(args)
218 return true
219 when 1
220 vim.enterMode('normal')
221 else
222 storage.count--
223 return false
224
225 }, {
226 exit: ({vim}) -> vim.enterMode('normal')
227 unquote: ({vim}) -> vim.enterMode('normal', {returnTo: 'ignore'})
228 })
229
230
231
232 mode('find', {
233 onEnter: ->
234
235 onLeave: ({vim}) ->
236 findBar = vim.window.gBrowser.getFindBar()
237 findStorage.lastSearchString = findBar._findField.value
238
239 onInput: (args, match) ->
240 args.findBar = args.vim.window.gBrowser.getFindBar()
241 if match.type == 'full'
242 match.command.run(args)
243 return true
244 return false
245
246 }, {
247 exit: ({findBar}) -> findBar.close()
248 })
249
250
251
252 mode('marks', {
253 onEnter: ({storage}, callback) ->
254 storage.callback = callback
255
256 onLeave: ({storage}) ->
257 storage.callback = null
258
259 onInput: (args, match) ->
260 {vim, storage} = args
261 storage.callback(match.keyStr)
262 vim.enterMode('normal')
263 return true
264 })
Imprint / Impressum