Scite 杂项脚本

lua-users home
wiki

此页面包含一些小的杂项 Scite 脚本

用于切换二进制值的简单脚本

这个简单的脚本切换光标下的单词。

function ToggleBinaryValue()
 local StartPos = editor.CurrentPos
 editor:WordRight()
 editor:WordLeftExtend()
 local Word = editor:GetSelText()
 
 if Word == "FALSE" then editor:ReplaceSel("TRUE") end
 if Word == "TRUE" then editor:ReplaceSel("FALSE") end
 if Word == "false" then editor:ReplaceSel("true") end
 if Word == "true" then editor:ReplaceSel("false") end
 if Word == "False" then editor:ReplaceSel("True") end
 if Word == "True" then editor:ReplaceSel("False") end
 if Word == "YES" then editor:ReplaceSel("NO") end
 if Word == "NO" then editor:ReplaceSel("YES") end
 if Word == "yes" then editor:ReplaceSel("no") end
 if Word == "no" then editor:ReplaceSel("yes") end
 if Word == "0" then editor:ReplaceSel("1") end
 if Word == "1" then editor:ReplaceSel("0") end
 
 editor:GotoPos(StartPos)
end
--DaveMDS

用于切换二进制值的复杂脚本

简单的脚本有一个缺陷,当一个切换词后面跟着一个或多个空格时。例如,“if (false)”将切换,而“if ( false )”则不会。这里有一个更复杂、更健壮的版本。这两个函数都需要,在你的 SciTE 选项文件中链接到 scite_ToggleBoolean()。

-- general lua function to alternatingly replace a bool-ish word in a string
function ToggleBoolean(str)
-- create a 2 colum table with toggle expressions
	local TogglePairTable = {}	
	TogglePairTable["FALSE"] = "TRUE"
	TogglePairTable["false"] = "true"
	TogglePairTable["False"] = "True"
	TogglePairTable["YES"] = "NO"
	TogglePairTable["yes"] = "no"
	TogglePairTable["0"] = "1"

-- replace left column string in table with righ column string
	for findString,replaceString in pairs(TogglePairTable) do
		if string.find(str, findString) then return string.gsub(str, findString, replaceString) end
	end
	
-- replace right column string in table with left column string	
	for replaceString,findString in pairs(TogglePairTable) do
		if string.find(str, findString) then return string.gsub(str, findString, replaceString) end
	end
	
	return str
end

-- For use in SciTE
-- this selects the the word under the caret
-- side effect: discards existing selection
function scite_ToggleBoolean()
	--save position
	local StartPos = editor.CurrentPos

	editor:WordRight()
	editor:WordLeftExtend() 
	local sel = editor:GetSelText()
	editor:ReplaceSel(ToggleBoolean(sel))
	
	-- reset position
	editor:SetSel(StartPos, StartPos) 
end

--SK

用于递增和递减数字的脚本

这个脚本递增和递减文本中找到的下一个数字。

function NumPlusPlus()
 output:ClearAll()
 local StartPos = editor.CurrentPos
 local CurLine = editor:LineFromPosition(StartPos)
 
 local fs,fe = editor:findtext("\-*[0-9]+", SCFIND_REGEXP,StartPos)
 editor:SetSel(fs,fe)
 local Number = editor:GetSelText()
 
 editor:ReplaceSel(Number + 1)
 editor:GotoPos(fs)
end

function NumMinusMinus()
 output:ClearAll()
 local StartPos = editor.CurrentPos
 local CurLine = editor:LineFromPosition(StartPos)
 
 local fs,fe = editor:findtext("\-*[0-9]+", SCFIND_REGEXP,StartPos)
 editor:SetSel(fs,fe)
 local Number = editor:GetSelText()
 
 editor:ReplaceSel(Number - 1)
 editor:GotoPos(fe)
end
--DaveMDS

用于转置两个字符的脚本

这个脚本翻转两个相邻字符的位置。

function transpose_characters()
  local pos = editor.CurrentPos
  editor:GotoPos(pos-1)
  editor.Anchor = pos+1
  local sel = editor:GetSelText()
  editor:ReplaceSel(string.sub(sel, 2, 2)..string.sub(sel, 1, 1))
  editor:GotoPos(pos)
end
--Myles Strous--

在光标位置插入当前日期

我在 SciTE 中错过了这个功能。将以下几行添加到用户选项文件 (SciTEUser.properties) 中

command.name.12.*=InsertDate
command.12.*=InsertDate
command.subsystem.12.*=3
command.mode.12.*=savebefore:no
command.shortcut.12.*=Ctrl+d

将以下几行添加到 Lua 启动脚本中

function InsertDate()
   editor:AddText(os.date("%Y-%m-%d"))
end

--Klaus Hummel--

此版本替换当前选择(如果有)

-- replace current selection with text
-- if there is none, insert at cursor position
function replaceOrInsert(text)
    local sel = editor:GetSelText()
    if string.len(sel) ~= 0 then
        editor:ReplaceSel(text)
    else
        editor:AddText(text)
    end
end

-- insert the current date in YYYY-mm-dd format
function insertDate()
    replaceOrInsert(os.date("%Y-%m-%d"))
end

另一个用于插入当前时间的函数

-- insert the current time in HH:MM:SS Timezone format
function insertTime()
    replaceOrInsert(os.date("%H:%M:%S %Z"))
end

--Chris Arndt--

在 .LOG 模式(类似记事本)中自动插入当前日期时间

将以下几行添加到用户选项文件 (SciTEUser.properties) 中

command.name.12.*=InsertDateTimeLog
command.12.*=InsertDateTimeLog
command.subsystem.12.*=3
command.mode.12.*=savebefore:no
command.shortcut.12.*=Enter

将以下几行添加到 Lua 启动脚本中

function InsertDateTimeLog()
   local Linea1, esLog, esLogMayus
   Linea1 = editor:GetLine(0)
   if Linea1 == nil then Linea1 = "0000" end
   esLog = string.sub(Linea1,1,4)
   esLogMayus = string.upper (esLog)
   if esLogMayus == ".LOG" then
      editor:AddText("\n\n--------------------\n")
      editor:AddText(os.date("%d.%b.%Y__%Hh:%Mm"))
      editor:AddText("\n--------------------\n")
   else editor:AddText("\n")
   end
end

--Barquero--

Lua 计算器/表达式求值器

一个简单的 Lua 表达式求值器,用于计算内容。突出显示有效的 Lua 表达式并运行脚本以执行计算或求值。

function SciTECalculator()
  local expr = editor:GetSelText()
  if not expr or expr == "" then return end
  local f, msg = loadstring("return "..expr)
  if not f then
    print(">Calculator: cannot evaluate selection") return
  end
  editor:ReplaceSel(tostring(f()))
end

--khman--

查找选择

local findText = editor:GetSelText()
local flag = 0

output:ClearAll()

if string.len(findText) > 0 then
	trace('>find: '..findText..'\n')
	local s,e = editor:findtext(findText,flag,0)
	local m = editor:LineFromPosition(s) - 1
	local count = 0

	while s do
		local l = editor:LineFromPosition(s)

		if l ~= m then
			count = count + 1

			local str = string.gsub(' '..editor:GetLine(l),'%s+',' ')

			local add = ':'..(l + 1)..':'
			local i = 8 - string.len(add)
			local ind = ' '
			while (string.len(ind) < i) do
				ind = ind..' '
			end

			trace(add..ind..str..'\n')
			m = l
		end

		s,e = editor:findtext(findText,flag,e+1)
	end

	trace('>result: '..count..'\n')
else
	trace('! Select symbol and replay')
end

--gans_A--

文本清理器

一个简单的脚本,可以消除粘贴文本中令人讨厌的无法识别的制表符(\t)和换行符(\r\n)字符。

function cleanText(reportNoMatch)
    editor:BeginUndoAction()
        for ln = 0, editor.LineCount - 1 do
            local lbeg = editor:PositionFromLine(ln)
            local lend = editor.LineEndPosition[ln]
            local text = editor:textrange(lbeg, lend)
            
            text = string.gsub(text, "\\t", "\t")
            text = string.gsub(text, "\\r", "\r")
            text = string.gsub(text, "\\n", "\n")
            
            editor.TargetStart = lbeg
            editor.TargetEnd = lend
            editor:ReplaceTarget(text)
        end--for
    editor:EndUndoAction()
end

--sirol81--

交换逗号

转置由逗号分隔的单词的位置。如果在上一句话中选择“when separated, of words”并交换,它现在将显示为“Transpose positions of words, when separated by commas”。它也适用于分号。

function swap_comma()
    local str = editor:GetSelText()
    local b = string.gsub(str,".*[,;]","")
    local c = string.gsub(str,".*([,;]).*","%1")
    local a = string.gsub(str,c..".*","")
    editor:ReplaceSel(b..c..a)
end

--hellork--


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2015 年 2 月 11 日凌晨 1:14 GMT (差异)