Jump to content

SciTE - Replace TAB with spaces in the open document


BugFix
 Share

Recommended Posts

When posting scripts to platforms that use different tab settings (e.g. GitHub), they are ripped from their formatting.

Thus, it is better to replace all tabs with spaces in the correct position before posting. I have created the following Lua script for this purpose. It replaces all tabs with the appropriate number of spaces in the document opened in SciTE. By default a tab width of 4 characters is used. But other values are also possible, details about this and the installation and usage are at the beginning of the script.

-- TIME_STAMP   2022-05-01 11:28:55   v 0.1

--[[
    == Installation ==
    • Store the file to "YOUR-PATH/TabReplaceSciTE.lua"

    • New entry in your "SciTEUser.properties"
     (find a free command number, in example is "49" used, and a free shortcut)

     #49 Replace TAB with spaces
     command.name.49.*=Replace TAB with spaces
     command.49.*=dofile "YOUR-PATH/TabReplaceSciTE.lua"
     command.mode.49.*=subsystem:lua,savebefore:no
     command.shortcut.49.*=Ctrl+Alt+Shift+R

    • If your sources has different values for TAB width, you can modify the command call 
      in this script (last line), "TabReplace_FileInSciTE(2)" or "TabReplace_FileInSciTE(8)".

      Or add a property to your "SciTEUser.properties" to have more flexibility:

      # The currently used tab.size, which is replaced by spaces
      # Without this property or with empty value "4" is used.
      tab.replace.width=2

      Then change the last line in this script to: TabReplace_FileInSciTE(props['tab.replace.width'])

    == Usage ==
    • Open any script.
    • Hit the shortcut.
    • In the opened document, all TAB will be replaced by the number of spaces corresponding to the TAB position in the line.
]]


----------------------------------------------------------------------------------------------------
--[[
in...:  _line       A line of text whose TAB are to be replaced by spaces.
.....:  _tabsize    TAB size in number of characters. If it is omitted, 4 is used.
out..:  The line, with TAB replaced if necessary, and the number of replacements.
]]
----------------------------------------------------------------------------------------------------
TabReplace_Line = function(_line, _tabsize)
    if _line:find('^[\r\n]+$') then return _line, 0 end -- only a line break
    if _line == '' then return _line, 0 end             -- only a empty string
    local posTab = _line:find('\t')
    if posTab == nil then return _line, 0 end           -- no TAB included
    _tabsize = _tabsize or 4                            -- default TAB width
    local tTab, s, sRep, iLen, sumLen = {}, ' ', '', 0, 0
    while posTab ~= nil do
        -- calculation replacement string, taking into account characters to be inserted
        iLen = (_tabsize - ((posTab + sumLen -1) % _tabsize))
        sumLen = sumLen + iLen -1                       -- total length of the replacements
        sRep = s:rep(iLen)                              -- create replacement string
        table.insert(tTab, sRep)                        -- save to table
        posTab = _line:find('\t', posTab +1)            -- find next TAB
    end
    local idx = 0
    _line = _line:gsub('\t', function() idx = idx +1 return tTab[idx] end)
    return _line, idx
end
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
--[[
    Replaces all TAB in the file currently open in SciTE
]]
----------------------------------------------------------------------------------------------------
TabReplace_FileInSciTE = function(_tabsize)
    local caret = editor.CurrentPos
    local fvl = editor.FirstVisibleLine
    local content = ''
    if _tabsize == '' then _tabsize = nil end
    for i=0, editor.LineCount -1 do
        local line = editor:GetLine(i)
        line = line or ''
        line = TabReplace_Line(line, _tabsize)
        content = content..line
    end
    editor:BeginUndoAction()
    editor:ClearAll()
    editor:InsertText(0, content)
    editor:EndUndoAction()
    editor.CurrentPos = caret
    editor:SetSel(caret, caret)
    editor.FirstVisibleLine = fvl
end
----------------------------------------------------------------------------------------------------

TabReplace_FileInSciTE(4)   -- If required: Change the TAB size here

 

TabReplaceSciTE.lua

Best Regards BugFix  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...