]> git.gir.st - VimFx.git/blob - extension/packages/modes.coffee
fix: make sure vim.blacklistedKeys is defined
[VimFx.git] / extension / packages / modes.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2013.
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 { mode_hints } = require 'mode-hints/mode-hints'
24 { updateToolbarButton } = require 'button'
25 { searchForMatchingCommand
26 , isEscCommandKey
27 , isReturnCommandKey
28 , findStorage } = require 'commands'
29
30 { interfaces: Ci } = Components
31
32 XULDocument = Ci.nsIDOMXULDocument
33
34 modes = {}
35
36 modes['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 isEditable = utils.isElementEditable(event.originalTarget)
46 autoInsertMode = isEditable or vim.rootWindow.TabView.isVisible()
47
48 if autoInsertMode and not isEscCommandKey(keyStr)
49 return false
50
51 storage.keys.push(keyStr)
52
53 { match, exact, command, count } = searchForMatchingCommand(storage.keys)
54
55 if vim.blacklistedKeys and storage.keys.join('') in vim.blacklistedKeys
56 match = false
57
58 if match
59 if exact
60 command.func(vim, event, count)
61 storage.keys.length = 0
62
63 # Esc key is not suppressed, and passed to the browser in normal mode.
64 #
65 # - It allows for stopping the loading of the page.
66 # - It allows for closing many custom dialogs (and perhaps other things
67 # -- Esc is a very commonly used key).
68 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
69 # an element. That allows for blurring an input in a custom dialog
70 # without closing the dialog too.
71 # - There are two reasons we might suppress it in other modes. If some
72 # custom dialog of a website is open, we should be able to cancel hint
73 # markers on it without closing it. Secondly, otherwise cancelling hint
74 # markers on Google causes its search bar to be focused.
75 # - It may only be suppressed in web pages, not in browser chrome. That
76 # allows for reseting the location bar when blurring it, and closing
77 # dialogs such as the “bookmark this page” dialog (<c-d>).
78 document = event.originalTarget.ownerDocument
79 inBrowserChrome = (document instanceof XULDocument)
80 if keyStr == 'Esc' and (not autoInsertMode or inBrowserChrome)
81 return false
82
83 return true
84
85 else
86 storage.keys.length = 0 unless /\d/.test(keyStr)
87
88 return false
89
90 modes['insert'] =
91 onEnter: (vim) ->
92 updateToolbarButton(vim.rootWindow, {insertMode: true})
93 onLeave: (vim) ->
94 updateToolbarButton(vim.rootWindow, {insertMode: false})
95 utils.blurActiveElement(vim.window)
96 onInput: (vim, storage, keyStr) ->
97 if isEscCommandKey(keyStr)
98 vim.enterMode('normal')
99 return true
100
101 modes['find'] =
102 onEnter: ->
103
104 onLeave: (vim) ->
105 findBar = vim.rootWindow.gBrowser.getFindBar()
106 findStorage.lastSearchString = findBar._findField.value
107
108 onInput: (vim, storage, keyStr) ->
109 findBar = vim.rootWindow.gBrowser.getFindBar()
110 if isEscCommandKey(keyStr) or keyStr == 'Return'
111 findBar.close()
112 return true
113 return false
114
115 modes['hints'] = mode_hints
116
117 exports.modes = modes
Imprint / Impressum