2 # Copyright Simon Lydell 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
20 # This file contains a few helper functions for Hints mode, that didn’t really
21 # fit in modes.coffee.
23 activateMatch = (vim, storage, match, matchedMarkers, callback) ->
24 {markerContainer} = storage
26 marker.markMatched(true) for marker in matchedMarkers
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
33 switchedMode = (vim.mode != 'hints')
35 if again and not switchedMode
37 vim.window.setTimeout((->
38 marker.markMatched(false) for marker in matchedMarkers
39 updateVisualFeedback(vim, markerContainer, [])
41 ), vim.options['hints.matched_timeout'])
42 markerContainer.reset()
45 vim.window.setTimeout((->
46 # Don’t clean up if Hints mode has been re-entered before the
48 cleanup(vim, storage) unless vim.mode == 'hints'
49 ), vim.options['hints.matched_timeout'])
52 storage.skipOnLeaveCleanup = true
53 vim._enterMode('normal')
54 storage.skipOnLeaveCleanup = false
56 cleanup = (vim, storage) ->
57 {markerContainer, matchText} = storage
58 markerContainer?.remove()
59 vim._run('clear_selection') if matchText and vim.mode != 'caret'
60 vim.hideNotification() if vim.options.notify_entered_keys
61 storage.clearInterval?()
66 getChar = (match, {markerContainer, matchText}) ->
67 {unmodifiedKey} = match
68 unmodifiedKey = unmodifiedKey.toLowerCase() unless matchText
73 when unmodifiedKey.length == 1
74 markerContainer.isHintChar(unmodifiedKey)
78 char = if isHintChar then unmodifiedKey else match.rawKey
80 return {char, isHintChar}
82 return {char: null, isHintChar: false}
84 updateVisualFeedback = (vim, markerContainer, visibleMarkers) ->
85 if vim.options.notify_entered_keys
86 if markerContainer.enteredText == ''
87 vim.hideNotification()
89 vim.notify(markerContainer.enteredText)
91 elementIndices = visibleMarkers.map((marker) -> marker.wrapper.elementIndex)
92 strings = markerContainer.splitEnteredText()
93 vim._send('highlightMarkableElements', {elementIndices, strings})