]> git.gir.st - VimFx.git/blob - extension/lib/hints-mode.coffee
Improve Hints mode in various ways
[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 unmodifiedKey = unmodifiedKey.toLowerCase() unless matchText
71
72 isHintChar = switch
73 when not matchText
74 true
75 when unmodifiedKey.length == 1
76 markerContainer.isHintChar(unmodifiedKey)
77 else
78 false
79
80 char = if isHintChar then unmodifiedKey else match.rawKey
81 if char.length == 1
82 return {char, isHintChar}
83 else
84 return {char: null, isHintChar: false}
85
86 updateVisualFeedback = (vim, markerContainer, visibleMarkers) ->
87 hasEnteredText = (markerContainer.enteredText != '')
88
89 if vim.options.notify_entered_keys
90 if hasEnteredText
91 vim.notify(markerContainer.enteredText)
92 else
93 vim.hideNotification()
94
95 elements = visibleMarkers.map((marker) ->
96 return {
97 elementIndex: marker.wrapper.elementIndex
98 selectAll: marker.highlighted and hasEnteredText
99 }
100 )
101 strings = markerContainer.splitEnteredText()
102 vim._send('highlightMarkableElements', {elements, strings})
103
104 isMatched = (visibleMarkers, {enteredHint}) ->
105 isUnique = (new Set(visibleMarkers.map((marker) -> marker.hint)).size == 1)
106 if isUnique
107 return {byText: true, byHint: (enteredHint == visibleMarkers[0].hint)}
108 else
109 return {byText: false, byHint: false}
110
111 module.exports = {
112 activateMatch
113 cleanup
114 getChar
115 updateVisualFeedback
116 isMatched
117 }
Imprint / Impressum