]> git.gir.st - VimFx.git/blob - extension/lib/modes.coffee
Fix #60: Give the same hint to links with the same URL
[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 utils = require('./utils')
23 { injectHints } = require('./hints')
24 { rotateOverlappingMarkers } = require('./marker')
25 { updateToolbarButton } = require('./button')
26 { commands
27 , searchForMatchingCommand
28 , escapeCommand
29 , Command
30 , findStorage } = require('./commands')
31
32 { interfaces: Ci } = Components
33
34 XULDocument = Ci.nsIDOMXULDocument
35
36 exports['normal'] =
37 onEnter: (vim, storage) ->
38 storage.keys ?= []
39 storage.commands ?= {}
40
41 onLeave: (vim, storage) ->
42 storage.keys.length = 0
43
44 onInput: (vim, storage, keyStr, event) ->
45 target = event.originalTarget
46 autoInsertMode = \
47 utils.isTextInputElement(target) or
48 utils.isContentEditable(target) or
49 (utils.isActivatable(target) and keyStr == '<enter>') or
50 (utils.isAdjustable(target) and keyStr in [
51 '<arrowup>', '<arrowdown>', '<arrowleft>', '<arrowright>'
52 '<space>', '<enter>'
53 ]) or
54 vim.rootWindow.TabView.isVisible()
55
56 storage.keys.push(keyStr)
57
58 { match, exact, command, count } = searchForMatchingCommand(storage.keys)
59
60 if vim.blacklistedKeys and storage.keys.join('') in vim.blacklistedKeys
61 match = false
62
63 if match
64
65 if autoInsertMode and command != escapeCommand
66 storage.keys.pop()
67 return false
68
69 if exact
70 command.func(vim, event, count)
71 storage.keys.length = 0
72
73 # Esc key is not suppressed, and passed to the browser in normal mode.
74 #
75 # - It allows for stopping the loading of the page.
76 # - It allows for closing many custom dialogs (and perhaps other things
77 # -- Esc is a very commonly used key).
78 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
79 # an element. That allows for blurring an input in a custom dialog
80 # without closing the dialog too.
81 # - There are two reasons we might suppress it in other modes. If some
82 # custom dialog of a website is open, we should be able to cancel hint
83 # markers on it without closing it. Secondly, otherwise cancelling hint
84 # markers on Google causes its search bar to be focused.
85 # - It may only be suppressed in web pages, not in browser chrome. That
86 # allows for reseting the location bar when blurring it, and closing
87 # dialogs such as the “bookmark this page” dialog (<c-d>).
88 document = event.originalTarget.ownerDocument
89 inBrowserChrome = (document instanceof XULDocument)
90 if keyStr == '<escape>' and (not autoInsertMode or inBrowserChrome)
91 return false
92
93 return true
94
95 else
96 storage.keys.length = 0 unless /\d/.test(keyStr)
97
98 return false
99
100 commands: commands
101
102 exports['insert'] =
103 onEnter: (vim, storage, count = null) ->
104 storage.count = count
105 updateToolbarButton(vim.rootWindow, {insertMode: true})
106 onLeave: (vim) ->
107 updateToolbarButton(vim.rootWindow, {insertMode: false})
108 utils.blurActiveElement(vim.window)
109 onInput: (vim, storage, keyStr) ->
110 switch storage.count
111 when null
112 if @commands['exit'].match(keyStr)
113 vim.enterMode('normal')
114 return true
115 when 1
116 vim.enterMode('normal')
117 else
118 storage.count--
119 return false
120 commands:
121 exit: ['<s-escape>']
122
123 exports['find'] =
124 onEnter: ->
125
126 onLeave: (vim) ->
127 findBar = vim.rootWindow.gBrowser.getFindBar()
128 findStorage.lastSearchString = findBar._findField.value
129
130 onInput: (vim, storage, keyStr) ->
131 findBar = vim.rootWindow.gBrowser.getFindBar()
132 if @commands['exit'].match(keyStr)
133 findBar.close()
134 return true
135 return false
136
137 commands:
138 exit: ['<escape>', '<enter>']
139
140 exports['hints'] =
141 onEnter: (vim, storage, filter, callback) ->
142 [ markers, container ] = injectHints(vim.rootWindow, vim.window, filter)
143 if markers.length > 0
144 storage.markers = markers
145 storage.container = container
146 storage.callback = callback
147 storage.numEnteredChars = 0
148 else
149 vim.enterMode('normal')
150
151 onLeave: (vim, storage) ->
152 { container } = storage
153 vim.rootWindow.setTimeout((->
154 container?.remove()
155 ), @timeout)
156 for key of storage
157 storage[key] = null
158
159 onInput: (vim, storage, keyStr, event) ->
160 { markers, callback } = storage
161
162 switch
163 when @commands['exit'].match(keyStr)
164 # Remove the hints immediately.
165 storage.container?.remove()
166 vim.enterMode('normal')
167 return true
168
169 when @commands['rotate_markers_forward'].match(keyStr)
170 rotateOverlappingMarkers(markers, true)
171 when @commands['rotate_markers_backward'].match(keyStr)
172 rotateOverlappingMarkers(markers, false)
173
174 when @commands['delete_hint_char'].match(keyStr)
175 for marker in markers
176 switch marker.hintIndex - storage.numEnteredChars
177 when 0 then marker.deleteHintChar()
178 when -1 then marker.show()
179 storage.numEnteredChars-- unless storage.numEnteredChars == 0
180
181 else
182 if keyStr not in utils.getHintChars()
183 return true
184 matchedMarkers = []
185 for marker in markers when marker.hintIndex == storage.numEnteredChars
186 match = marker.matchHintChar(keyStr)
187 marker.hide() unless match
188 if marker.isMatched()
189 marker.markMatched(true)
190 matchedMarkers.push(marker)
191 if matchedMarkers.length > 0
192 again = callback(matchedMarkers[0])
193 if again
194 vim.rootWindow.setTimeout((->
195 marker.markMatched(false) for marker in matchedMarkers
196 ), @timeout)
197 marker.reset() for marker in markers
198 storage.numEnteredChars = 0
199 else
200 vim.enterMode('normal')
201 return true
202 storage.numEnteredChars++
203
204 return true
205
206 timeout: 200
207
208 commands:
209 exit: ['<escape>']
210 rotate_markers_forward: ['<space>']
211 rotate_markers_backward: ['<s-space>']
212 delete_hint_char: ['<backspace>']
213
214 for modeName of exports
215 mode = exports[modeName]
216 continue if Array.isArray(mode.commands)
217 for commandName of mode.commands
218 name = "mode_#{ modeName }_#{ commandName }"
219 keys = mode.commands[commandName].map((key) -> [key])
220 mode.commands[commandName] = new Command(null, name, null, keys)
Imprint / Impressum