Last active
October 15, 2021 08:05
-
-
Save pkulchenko/70424f7660fb4465ae43 to your computer and use it in GitHub Desktop.
Highlight selected plugin for ZeroBrane Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Copyright 2015 Paul Kulchenko, ZeroBrane LLC; All rights reserved | |
local updateneeded | |
local indicator = 12 | |
return { | |
name = "Highlight selected", | |
description = "Highlights all instances of a selected word.", | |
author = "Paul Kulchenko", | |
version = 0.11, | |
dependencies = 0.71, | |
onEditorUpdateUI = function(self, editor, event) | |
if event:GetUpdated() == wxstc.wxSTC_UPDATE_SELECTION then | |
updateneeded = editor | |
end | |
end, | |
onIdle = function(self) | |
if not updateneeded then return end | |
local editor = updateneeded | |
updateneeded = false | |
local length, curpos = editor:GetLength(), editor:GetCurrentPos() | |
local value = editor:GetTextRange(editor:GetSelectionStart(), editor:GetSelectionEnd()) | |
if #value == 0 or not value:find('%w') then | |
editor:SetIndicatorCurrent(indicator) | |
editor:IndicatorClearRange(0, length) | |
return | |
end | |
local word = editor:GetTextRange( -- try to select a word under caret | |
editor:WordStartPosition(curpos, true), editor:WordEndPosition(curpos, true)) | |
if #word == 0 then | |
word = editor:GetTextRange( -- try to select a non-word under caret | |
editor:WordStartPosition(curpos, false), editor:WordEndPosition(curpos, false)) | |
end | |
if value ~= word then return end | |
local style = bit.band(editor:GetStyleAt(editor:GetSelectionStart()),31) | |
local color = editor:StyleGetForeground(style) | |
editor:IndicatorSetStyle(indicator, wxstc.wxSTC_INDIC_BOX) | |
editor:IndicatorSetForeground(indicator, color) | |
editor:SetIndicatorCurrent(indicator) | |
editor:IndicatorClearRange(0, length) | |
editor:SetSearchFlags(wxstc.wxSTC_FIND_WHOLEWORD + wxstc.wxSTC_FIND_MATCHCASE) | |
local pos = 0 | |
while true do | |
editor:SetTargetStart(pos) | |
editor:SetTargetEnd(length) | |
pos = editor:SearchInTarget(value) | |
if pos == -1 then break end | |
editor:IndicatorFillRange(pos, #value) | |
pos = pos + #value | |
end | |
end, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thank you so much!