]> git.gir.st - VimFx.git/blob - extension/lib/modes.coffee
Merge pull request #551 from akhodakivskiy/late-shortcuts
[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 }, wrappers, viewport, callback, count = 1) ->
108 [ markers, container ] = hints.injectHints(
109 vim.window, wrappers, viewport, vim.options
110 )
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
117 else
118 vim.enterMode('normal')
119
120 onLeave: ({ vim, storage }) ->
121 { container } = storage
122 vim.window.setTimeout((->
123 container?.remove()
124 ), vim.options.hints_timeout)
125 for key of storage
126 storage[key] = null
127
128 onInput: (args, match) ->
129 { vim, storage } = args
130 { markers, callback } = storage
131
132 if match.type == 'full'
133 match.command.run(args)
134 else if match.unmodifiedKey in vim.options.hint_chars
135 matchedMarkers = []
136
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)
143
144 if matchedMarkers.length > 0
145 again = callback(matchedMarkers[0], storage.count, match.keyStr)
146 storage.count--
147 if again
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
153 else
154 vim.enterMode('normal')
155 else
156 storage.numEnteredChars++
157
158 return true
159
160 }, {
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')
166
167 rotate_markers_forward: ({ storage }) ->
168 rotateOverlappingMarkers(storage.markers, true)
169
170 rotate_markers_backward: ({ storage }) ->
171 rotateOverlappingMarkers(storage.markers, false)
172
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
179
180 increase_count: ({ storage }) -> storage.count++
181 })
182
183
184
185 mode('ignore', {
186 onEnter: ({ vim, storage }, count = null) ->
187 storage.count = count
188
189 onLeave: ({ vim, storage }) ->
190 vim._run('blur_active_element') unless storage.count?
191
192 onInput: (args, match) ->
193 { vim, storage } = args
194 switch storage.count
195 when null
196 if match.type == 'full'
197 match.command.run(args)
198 return true
199 when 1
200 vim.enterMode('normal')
201 else
202 storage.count--
203 return false
204
205 }, {
206 exit: ({ vim }) -> vim.enterMode('normal')
207 unquote: ({ vim }) -> vim.enterMode('normal', {returnTo: 'ignore'})
208 })
209
210
211
212 mode('find', {
213 onEnter: ->
214
215 onLeave: ({ vim }) ->
216 findBar = vim.window.gBrowser.getFindBar()
217 findStorage.lastSearchString = findBar._findField.value
218
219 onInput: (args, match) ->
220 args.findBar = args.vim.window.gBrowser.getFindBar()
221 if match.type == 'full'
222 match.command.run(args)
223 return true
224 return false
225
226 }, {
227 exit: ({ findBar }) -> findBar.close()
228 })
Imprint / Impressum