]> git.gir.st - VimFx.git/blob - extension/lib/hints-mode.coffee
Always treat the hint chars' case as-is
[VimFx.git] / extension / lib / hints-mode.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file contains a few helper functions for Hints mode, that didn’t really
21 # fit in modes.coffee.
22
23 activateMatch = (vim, storage, match, matchedMarkers, callback) ->
24 {markerContainer} = storage
25
26 marker.markMatched(true) for marker in matchedMarkers
27
28 # Prevent `onLeave` cleanup if the callback enters another mode.
29 storage.skipOnLeaveCleanup = true
30 again = callback(matchedMarkers[0], storage.count, match.keyStr)
31 storage.skipOnLeaveCleanup = false
32
33 switchedMode = (vim.mode != 'hints')
34
35 if again and not switchedMode
36 storage.count -= 1
37 vim.window.setTimeout((->
38 marker.markMatched(false) for marker in matchedMarkers
39 updateVisualFeedback(vim, markerContainer, [])
40 return
41 ), vim.options['hints.matched_timeout'])
42 markerContainer.reset()
43
44 else
45 vim.window.setTimeout((->
46 # Don’t clean up if Hints mode has been re-entered before the
47 # timeout has passed.
48 cleanup(vim, storage) unless vim.mode == 'hints'
49 ), vim.options['hints.matched_timeout'])
50
51 unless switchedMode
52 storage.skipOnLeaveCleanup = true
53 vim._enterMode('normal')
54 storage.skipOnLeaveCleanup = false
55
56 cleanup = (vim, storage) ->
57 {markerContainer, matchText} = storage
58 markerContainer?.remove()
59 vim._run('clear_selection') if matchText and vim.mode != 'caret'
60 if vim.options.notify_entered_keys and
61 markerContainer.enteredText == vim._state.lastNotification
62 vim.hideNotification()
63 storage.clearInterval?()
64 for key of storage
65 storage[key] = null
66 return
67
68 getChar = (match, {markerContainer, matchText}) ->
69 {unmodifiedKey} = match
70
71 isHintChar = switch
72 when not matchText
73 true
74 when unmodifiedKey.length == 1
75 markerContainer.isHintChar(unmodifiedKey)
76 else
77 false
78
79 char = if isHintChar then unmodifiedKey else match.rawKey
80 if char.length == 1
81 return {char, isHintChar}
82 else
83 return {char: null, isHintChar: false}
84
85 updateVisualFeedback = (vim, markerContainer, visibleMarkers) ->
86 hasEnteredText = (markerContainer.enteredText != '')
87
88 if vim.options.notify_entered_keys
89 if hasEnteredText
90 vim._notifyPersistent(markerContainer.enteredText)
91 else
92 vim.hideNotification()
93
94 elements = visibleMarkers.map((marker) ->
95 return {
96 elementIndex: marker.wrapper.elementIndex
97 selectAll: marker.highlighted and hasEnteredText
98 }
99 )
100 strings = markerContainer.splitEnteredText()
101 vim._send('highlightMarkableElements', {elements, strings})
102
103 isMatched = (visibleMarkers, {enteredHint}) ->
104 isUnique = (new Set(visibleMarkers.map((marker) -> marker.hint)).size == 1)
105 if isUnique
106 return {byText: true, byHint: (enteredHint == visibleMarkers[0].hint)}
107 else
108 return {byText: false, byHint: false}
109
110 module.exports = {
111 activateMatch
112 cleanup
113 getChar
114 updateVisualFeedback
115 isMatched
116 }
Imprint / Impressum