]> git.gir.st - VimFx.git/blob - extension/packages/modes.coffee
Add count support to tab switching commands
[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, count } = searchForMatchingCommand(storage.keys)
29
30 if match
31 if exact
32 command.func(vim, event, count)
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 unless /\d/.test(keyStr)
54
55 return false
56
57 modes['insert'] =
58 onEnter: (vim) ->
59 updateToolbarButton(vim.rootWindow, {insertMode: true})
60 onLeave: (vim) ->
61 updateToolbarButton(vim.rootWindow, {insertMode: false})
62 utils.blurActiveElement(vim.window)
63 onInput: (vim, storage, keyStr) ->
64 if isEscCommandKey(keyStr)
65 vim.enterMode('normal')
66 return true
67
68 modes['find'] =
69 onEnter: ->
70
71 onLeave: (vim) ->
72 findBar = vim.rootWindow.gBrowser.getFindBar()
73 findStorage.lastSearchString = findBar._findField.value
74
75 onInput: (vim, storage, keyStr) ->
76 findBar = vim.rootWindow.gBrowser.getFindBar()
77 if isEscCommandKey(keyStr) or keyStr == 'Return'
78 findBar.close()
79 return true
80 return false
81
82 modes['hints'] = mode_hints
83
84 exports.modes = modes
Imprint / Impressum