mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 06:37:14 +09:00
Support for highlighting all search matches (hlsearch) (#1762)
* Support for highlighting all search matches (hlsearch) hlsearch is implemented efficiently using the buffer's line array, somewhat similarly to the syntax highlighting. Unlike the syntax highlighter which highlights the entire file, hlsearch searches for matches for the displayed lines only. Matches are searched when the given line is displayed first time or after it was modified. Otherwise the previously found matches are used. * Add UnhighlightSearch action and add it to the list of actions triggered by Esc key by default. * Add comment explaining the purpose of search map * Add hlsearch colors to colorschemes Mostly just copied from the corresponding original (mostly vim) colorschemes. * Highlight matches during/after replace as well As a side effect it also changes the last search value, i.e. affects FindNext and FindPrevious, but it's probably fine. In vim it works the same way. * Improve hlsearch option description
This commit is contained in:
@@ -864,8 +864,10 @@ func (h *BufPane) FindLiteral() bool {
|
||||
|
||||
// Search searches for a given string/regex in the buffer and selects the next
|
||||
// match if a match is found
|
||||
// This function affects lastSearch and lastSearchRegex (saved searches) for
|
||||
// use with FindNext and FindPrevious
|
||||
// This function behaves the same way as Find and FindLiteral actions:
|
||||
// it affects the buffer's LastSearch and LastSearchRegex (saved searches)
|
||||
// for use with FindNext and FindPrevious, and turns HighlightSearch on or off
|
||||
// according to hlsearch setting
|
||||
func (h *BufPane) Search(str string, useRegex bool, searchDown bool) error {
|
||||
match, found, err := h.Buf.FindNext(str, h.Buf.Start(), h.Buf.End(), h.Cursor.Loc, searchDown, useRegex)
|
||||
if err != nil {
|
||||
@@ -877,8 +879,9 @@ func (h *BufPane) Search(str string, useRegex bool, searchDown bool) error {
|
||||
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
|
||||
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
|
||||
h.Cursor.GotoLoc(h.Cursor.CurSelection[1])
|
||||
h.lastSearch = str
|
||||
h.lastSearchRegex = useRegex
|
||||
h.Buf.LastSearch = str
|
||||
h.Buf.LastSearchRegex = useRegex
|
||||
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
|
||||
h.Relocate()
|
||||
} else {
|
||||
h.Cursor.ResetSelection()
|
||||
@@ -922,8 +925,9 @@ func (h *BufPane) find(useRegex bool) bool {
|
||||
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
|
||||
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
|
||||
h.Cursor.GotoLoc(h.Cursor.CurSelection[1])
|
||||
h.lastSearch = resp
|
||||
h.lastSearchRegex = useRegex
|
||||
h.Buf.LastSearch = resp
|
||||
h.Buf.LastSearchRegex = useRegex
|
||||
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
|
||||
} else {
|
||||
h.Cursor.ResetSelection()
|
||||
InfoBar.Message("No matches found")
|
||||
@@ -944,6 +948,18 @@ func (h *BufPane) find(useRegex bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ToggleHighlightSearch toggles highlighting all instances of the last used search term
|
||||
func (h *BufPane) ToggleHighlightSearch() bool {
|
||||
h.Buf.HighlightSearch = !h.Buf.HighlightSearch
|
||||
return true
|
||||
}
|
||||
|
||||
// UnhighlightSearch unhighlights all instances of the last used search term
|
||||
func (h *BufPane) UnhighlightSearch() bool {
|
||||
h.Buf.HighlightSearch = false
|
||||
return true
|
||||
}
|
||||
|
||||
// FindNext searches forwards for the last used search term
|
||||
func (h *BufPane) FindNext() bool {
|
||||
// If the cursor is at the start of a selection and we search we want
|
||||
@@ -954,7 +970,7 @@ func (h *BufPane) FindNext() bool {
|
||||
if h.Cursor.HasSelection() {
|
||||
searchLoc = h.Cursor.CurSelection[1]
|
||||
}
|
||||
match, found, err := h.Buf.FindNext(h.lastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, true, h.lastSearchRegex)
|
||||
match, found, err := h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, true, h.Buf.LastSearchRegex)
|
||||
if err != nil {
|
||||
InfoBar.Error(err)
|
||||
}
|
||||
@@ -981,7 +997,7 @@ func (h *BufPane) FindPrevious() bool {
|
||||
if h.Cursor.HasSelection() {
|
||||
searchLoc = h.Cursor.CurSelection[0]
|
||||
}
|
||||
match, found, err := h.Buf.FindNext(h.lastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, false, h.lastSearchRegex)
|
||||
match, found, err := h.Buf.FindNext(h.Buf.LastSearch, h.Buf.Start(), h.Buf.End(), searchLoc, false, h.Buf.LastSearchRegex)
|
||||
if err != nil {
|
||||
InfoBar.Error(err)
|
||||
}
|
||||
|
||||
@@ -226,9 +226,6 @@ type BufPane struct {
|
||||
// Same here, just to keep track for mouse move events
|
||||
tripleClick bool
|
||||
|
||||
// Last search stores the last successful search for FindNext and FindPrev
|
||||
lastSearch string
|
||||
lastSearchRegex bool
|
||||
// Should the current multiple cursor selection search based on word or
|
||||
// based on selection (false for selection, true for word)
|
||||
multiWord bool
|
||||
@@ -683,6 +680,8 @@ var BufKeyActions = map[string]BufKeyAction{
|
||||
"ToggleKeyMenu": (*BufPane).ToggleKeyMenu,
|
||||
"ToggleDiffGutter": (*BufPane).ToggleDiffGutter,
|
||||
"ToggleRuler": (*BufPane).ToggleRuler,
|
||||
"ToggleHighlightSearch": (*BufPane).ToggleHighlightSearch,
|
||||
"UnhighlightSearch": (*BufPane).UnhighlightSearch,
|
||||
"ClearStatus": (*BufPane).ClearStatus,
|
||||
"ShellMode": (*BufPane).ShellMode,
|
||||
"CommandMode": (*BufPane).CommandMode,
|
||||
|
||||
@@ -835,6 +835,9 @@ func (h *BufPane) ReplaceCmd(args []string) {
|
||||
h.Cursor.SetSelectionStart(locs[0])
|
||||
h.Cursor.SetSelectionEnd(locs[1])
|
||||
h.Cursor.GotoLoc(locs[0])
|
||||
h.Buf.LastSearch = search
|
||||
h.Buf.LastSearchRegex = true
|
||||
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
|
||||
|
||||
h.Relocate()
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ var bufdefaults = map[string]string{
|
||||
"F4": "Quit",
|
||||
"F7": "Find",
|
||||
"F10": "Quit",
|
||||
"Esc": "Escape,Deselect,ClearInfo,RemoveAllMultiCursors",
|
||||
"Esc": "Escape,Deselect,ClearInfo,RemoveAllMultiCursors,UnhighlightSearch",
|
||||
|
||||
// Mouse bindings
|
||||
"MouseWheelUp": "ScrollUp",
|
||||
|
||||
@@ -89,7 +89,7 @@ var bufdefaults = map[string]string{
|
||||
"F4": "Quit",
|
||||
"F7": "Find",
|
||||
"F10": "Quit",
|
||||
"Esc": "Escape,Deselect,ClearInfo,RemoveAllMultiCursors",
|
||||
"Esc": "Escape,Deselect,ClearInfo,RemoveAllMultiCursors,UnhighlightSearch",
|
||||
|
||||
// Mouse bindings
|
||||
"MouseWheelUp": "ScrollUp",
|
||||
|
||||
Reference in New Issue
Block a user