]> git.gir.st - VimFx.git/blob - extension/packages/modes.coffee
Merge pull request #343 from lydell/better-find-mode
[VimFx.git] / extension / packages / modes.coffee
1 utils = require 'utils'
2 { mode_hints } = require 'mode-hints/mode-hints'
3 { updateToolbarButton } = require 'button'
4 { searchForMatchingCommand
5 , isEscCommandKey
6 , isReturnCommandKey
7 , findStorage } = require 'commands'
8
9 modes = {}
10
11 modes['normal'] =
12 onEnter: (vim, storage) ->
13 storage.keys ?= []
14 storage.commands ?= {}
15
16 onLeave: (vim, storage) ->
17 storage.keys.length = 0
18
19 onInput: (vim, storage, keyStr, event) ->
20 isEditable = utils.isElementEditable(event.originalTarget)
21 autoInsertMode = isEditable or vim.rootWindow.TabView.isVisible()
22
23 if autoInsertMode and not isEscCommandKey(keyStr)
24 return false
25
26 storage.keys.push(keyStr)
27
28 { match, exact, command } = searchForMatchingCommand(storage.keys)
29
30 if match
31 if exact
32 command.func(vim, event)
33 storage.keys.length = 0
34
35 # Esc key is not suppressed, and passed to the browser in normal mode.
36 #
37 # - It allows for stopping the loading of the page.
38 # - It allows for closing many custom dialogs (and perhaps other things
39 # -- Esc is a very commonly used key).
40 # - It is not passed if Esc is used for `command_Esc` and we’re blurring
41 # an element. That allows for blurring an input in a custom dialog
42 # without closing the dialog too.
43 # - There are two reasons we might suppress it in other modes. If some
44 # custom dialog of a website is open, we should be able to cancel hint
45 # markers on it without closing it. Secondly, otherwise cancelling hint
46 # markers on Google causes its search bar to be focused.
47 if keyStr == 'Esc' and not autoInsertMode
48 return false
49
50 return true
51
52 else
53 storage.keys.length = 0
54 return false
55
56 modes['insert'] =
57 onEnter: (vim) ->
58 updateToolbarButton(vim.rootWindow, {insertMode: true})
59 onLeave: (vim) ->
60 updateToolbarButton(vim.rootWindow, {insertMode: false})
61 utils.blurActiveElement(vim.window)
62 onInput: (vim, storage, keyStr) ->
63 if isEscCommandKey(keyStr)
64 vim.enterMode('normal')
65 return true
66
67 modes['find'] =
68 onEnter: ->
69
70 onLeave: (vim) ->
71 findBar = vim.rootWindow.gBrowser.getFindBar()
72 findStorage.lastSearchString = findBar._findField.value
73
74 onInput: (vim, storage, keyStr) ->
75 findBar = vim.rootWindow.gBrowser.getFindBar()
76 if isEscCommandKey(keyStr) or keyStr == 'Return'
77 findBar.close()
78 return true
79 return false
80
81 modes['hints'] = mode_hints
82
83 exports.modes = modes
Imprint / Impressum