dotfiles/.config/kak/autoload/search-highlight.kak

115 lines
4.8 KiB
Text

## kak-search-highlight: interactive highlighting of search terms
## Author: Daniel Fichtinger <daniel@ficd.sh>
## License: ISC
provide-module search-highlight %—
## begin public options
declare-option -docstring %{
Face definition for search highlighting. See :doc faces.
} str search_highlight_face rgb:D87C4A+i
declare-option -docstring %{
Whether escaping the search prompt should clear the highlighting.
} bool search_highlight_prompt_escape false
declare-option -docstring %{
Whether the highlighting should always be cleared when returning to
normal mode. Takes priority over search_highlight_prompt_escape.
} bool search_highlight_auto_clear false
## end
# track current highlighting status
declare-option -hidden str search_highlight_status 'search-highlight-off'
def -hidden -override search-highlight-on nop
def -hidden -override search-highlight-off fail
def -hidden -override true nop
def -hidden -override false fail
declare-option -hidden bool search_highlight_quit false
define-command -hidden search-highlight-clear %{
try %{
# for safety; only run if highlighting is on
%opt{search_highlight_status}
# remove highlighter
remove-highlighter window/search-highlight
# update status
set-option window search_highlight_status 'search-highlight-off'
}
}
# enable the plugin
define-command -hidden search-highlight-enable-impl %{
# we only watch the search register in / and reverse / searches
hook -group search-highlight window NormalKey (/|<a-/>) %{
# register hook to show highlighting during search
hook -group search-highlight-impl window RegisterModified '/' %{
# check whether already enabled
try %{
%opt{search_highlight_status}
} catch %{
try %{
# if the user escapes search, we should immediately clear the highlight
# in normal mode so we set the flag before escaping
# but only if the option is set
%opt{search_highlight_prompt_escape}
map window prompt <esc> '<a-;>: set-option window search_highlight_quit true<ret><esc>'
}
# if not already enabled, add the highlighter
add-highlighter window/search-highlight dynregex '%reg{/}' %exp{0:%opt{search_highlight_face}}
# update status tracker
set-option window search_highlight_status 'search-highlight-on'
# register hook to clear highlighter after leaving search
hook -once -group search-highlight window NormalIdle .* %{
# stop watching the search register
# (we've left search now)
remove-hooks window search-highlight-impl
# check if auto clear is set
try %{
%opt{search_highlight_auto_clear}
search-highlight-clear
} catch %{
# check if user escaped search
try %{
%opt{search_highlight_quit}
# if so, clear immediately
search-highlight-clear
} catch %{
# otherwise, we clear on escape
hook -once -group search-highlight window NormalKey <esc> %{
search-highlight-clear
}
}
}
# clear the prompt mapping so it doesn't conflict with
# other prompt types
unmap window prompt <esc>
# reset the quit-early flag
set-option window search_highlight_quit false
}
}
}
}
}
# remove the plugin's hooks
define-command -hidden search-highlight-disable-impl %{
remove-hooks window search-highlight(-impl)?
}
## begin public commands
define-command -docstring %{
Enable search highlighting. Press <esc> in normal mode to clear
highlighting of the current search.
Respects the search_highlight_face option.
} search-highlight-enable %{
search-highlight-enable-impl
}
define-command -docstring %{
Disable search highlighting. Respects the search_highlight_face
option.
} search-highlight-disable %{
search-highlight-disable-impl
}
## end