----------------------------------------------------------------------- -- Tabs to spaces (EditPad-style) for SciTE (self-contained) -- Kein-Hong Man 20040727 -- This program is hereby placed into PUBLIC DOMAIN ----------------------------------------------------------------------- -- Best installed as a shortcut key, e.g. Ctrl-1 with the following in -- your user properties file, usually SciTEUser.properties: -- command.name.1.*=Tabs to Spaces -- command.subsystem.1.*=3 -- command.1.*=TabToSpace -- command.save.before.1.*=2 -- This Lua function itself should be in SciTEStartup.lua, assuming it -- is properly set up. Consult SciTEDoc.html if you are unsure of what -- to do. You can also keep it in a separate file and load it using: -- require(props["SciteUserHome"].."/SciTE_TabSpace.lua") -- Tested on SciTE 1.61+ (CVS 20040718) on Win98SE. Be careful when -- tweaking it, you *may* be able to crash SciTE. You have been warned! ----------------------------------------------------------------------- -- This is useful if you often copy text over to a word processor for -- printing. Word processors have tabs based on physical lengths, so -- indentation of source code would usually start to run. The current -- tab width can be overridden using a global property: -- ext.lua.tabtospace.tabsize=8 ----------------------------------------------------------------------- TabToSpace = function() -- get override or editor's current tab size local tab = tonumber(props["ext.lua.tabtospace.tabsize"]) if not tab then tab = editor.TabWidth end 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) local changed = false while true do local x, y = string.find(text, "\t", 1, 1) if x then -- found tab, replace local z = x - 1 + tab y = z - math.mod(z, tab) + 1 -- tab stop position text = string.sub(text, 1, x - 1) .. string.rep(" ", y - x) .. string.sub(text, x + 1) changed = true else -- no more tabs break end end--while if changed then editor.TargetStart = lbeg editor.TargetEnd = lend editor:ReplaceTarget(text) end end--for editor:EndUndoAction() end ----------------------------------------------------------------------- -- Spaces to tabs (EditPad-style) for SciTE (self-contained) -- Kein-Hong Man 20040727 -- This program is hereby placed into PUBLIC DOMAIN ----------------------------------------------------------------------- -- Best installed as a shortcut key, e.g. Ctrl-1 with the following in -- your user properties file, usually SciTEUser.properties: -- command.name.1.*=Spaces to Tabs -- command.subsystem.1.*=3 -- command.1.*=SpaceToTab -- command.save.before.1.*=2 -- This Lua function itself should be in SciTEStartup.lua, assuming it -- is properly set up. Consult SciTEDoc.html if you are unsure of what -- to do. You can also keep it in a separate file and load it using: -- require(props["SciteUserHome"].."/SciTE_TabSpace.lua") -- Tested on SciTE 1.61+ (CVS 20040718) on Win98SE. Be careful when -- tweaking it, you *may* be able to crash SciTE. You have been warned! ----------------------------------------------------------------------- -- Converts spaces to tabs, based on the current or given tab size. -- Removes redundant spaces within sentences as well. Converts trailing -- spaces to tabs where appropriate, but does not delete them. The -- current tab width can be overridden using a global property: -- ext.lua.spacetotab.tabsize=8 ----------------------------------------------------------------------- SpaceToTab = function() -- get override or editor's current tab size local tab = tonumber(props["ext.lua.spacetotab.tabsize"]) if not tab then tab = editor.TabWidth end -- computes next tab stop, using column value from 0 local SpaceToNextStop = function(col) local z = col + tab return tab - math.mod(z, tab) end -- loop for each line 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) local changed = false local x = 1, y, z local col = 0 while x <= string.len(text) do c = string.byte(text, x) if c == 9 then -- tab col = col + SpaceToNextStop(col) x = x + 1 elseif c == 32 then -- space y, z = string.find(text, " +", x) y = z - y + 1 -- number of spaces found z = SpaceToNextStop(col) if y >= z then -- if spaces past a tab stop, convert to a single tab if z > 1 then -- convert spaces to tab if >1 space col = col + z text = string.sub(text, 1, x - 1) .. "\t" .. string.sub(text, x + z) x = x + 1 changed = true else -- leave a single space be x = x + 1 col = col + 1 end else -- y < z -- if there's a following tab, spaces are redundant if string.byte(text, x + y) == 9 then -- got a tab col = col + z text = string.sub(text, 1, x - 1) .. string.sub(text, x + y) changed = true else -- no following tab, leave them be x = x + y col = col + y end end else -- normal char x = x + 1 col = col + 1 end end--while if changed then editor.TargetStart = lbeg editor.TargetEnd = lend editor:ReplaceTarget(text) end end--for editor:EndUndoAction() end -- end of script