Scite Misc Scripts

lua-users home
wiki

This page contains some small miscellaneous SciteScripts.

Simple script for toggle binary value

This simple script toggle the word under the cursor.

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

Script for increment and decrement number

This script increment and decrement the next number found in text.

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

Script for transposing two characters

This script flips the positions of two adjacent characters.

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--

Insert the current date at the cursor position

I missed this feature in SciTE. Add the following lines into User Options File (SciTEUser.properties)

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

Add the following lines into Lua Startup Script:

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

--Klaus Hummel--

This version replaces the current selection, if there is one:

-- 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

And another function for inserting the current time:

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

--Chris Arndt--

Lua calculator/expression evaluator

A simple Lua expression evaluator for calculating stuff. Highlight a valid Lua expression and run the script to perform the calculation or evaluation.

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--

Find selection

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--


FindPage · RecentChanges · preferences
edit · history
Last edited August 31, 2006 7:54 pm GMT (diff)