Skip to content

Instantly share code, notes, and snippets.

@EngineerSmith
Created April 23, 2022 16:02
Show Gist options
  • Save EngineerSmith/98bc448c3af912ab3ddc1207214a0b2b to your computer and use it in GitHub Desktop.
Save EngineerSmith/98bc448c3af912ab3ddc1207214a0b2b to your computer and use it in GitHub Desktop.
Zerobrane plugins
-- 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,
}
return {
name = "Move line up/down",
description = "Adds moving line or selection up or down using `Ctrl-Shift-Up/Down`.",
author = "Paul Kulchenko",
version = 0.11,
onEditorKeyDown = function(self, editor, event)
local key = event:GetKeyCode()
local mod = event:GetModifiers()
if (key == wx.WXK_UP or key == wx.WXK_DOWN)
and (mod == wx.wxMOD_CONTROL + wx.wxMOD_SHIFT) then
local line1 = editor:LineFromPosition(editor:GetSelectionStart())
local line2 = editor:LineFromPosition(editor:GetSelectionEnd())
local cut, insert
if key == wx.WXK_UP and line1 > 0 then
cut, insert = line1-1, line2
elseif key == wx.WXK_DOWN and line2 < editor:GetLineCount()-1 then
insert, cut = line1, line2+1
else
return
end
local line = editor:GetLine(cut)
editor:BeginUndoAction()
editor:DeleteRange(editor:PositionFromLine(cut), #line)
local pos = editor:PositionFromLine(insert)
local current, anchor = editor:GetCurrentPos(), editor:GetAnchor()
-- inserting at current position requires a fix as the cursor is
-- anchored to the beginning of the line, which won't move
if pos == current then editor:SetCurrentPos(current+1) end
if pos == anchor then editor:SetAnchor(anchor+1) end
editor:SetTargetStart(pos)
editor:SetTargetEnd(pos)
editor:ReplaceTarget(line)
if pos == current then editor:SetCurrentPos(editor:GetCurrentPos()-1) end
if pos == anchor then editor:SetAnchor(editor:GetAnchor()-1) end
editor:EnsureCaretVisible()
editor:EndUndoAction()
return false -- don't apply "default" handling
end
end,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment