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 Options 檔案中會需要兩個函式,連結到 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--

文字整理器

一個簡單的指令碼,用來移除貼上文字中令人討厭且無法辨識的 tab(\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--

交換逗號

在逗號分隔的文字中置換單字的順序。若選取前一個句子中的「在逗號分隔的文字中」並交換順序,現在將變成「交換逗號分隔的文字中的單字」。它也可以用在分號上。

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 (差異)