Example Script [Not working]: Align Columns

Started by Stefan, September 27, 2009, 08:57:39 PM

Previous topic - Next topic

Stefan

Hi all.

To see what is possible by an script
and what is needed from the editor script engine
i post here some test scripts.

Please note that this scripts are NOT written for HippoEDIT right now
and that HippoEDIT didn't even have an scripting support implemented right now.
(script will be converted once HippoEDIT scripting support is implemented)

Hint:
* i am not an programmer,
* that script may not be well designed (mostly i am have less interest if my script works "fair to middling"
    to clean it up, because of i had to do other things is more important mostly)
* test my scripts with test files only
* i give no guarantee that your files are not deleted
   and that your computer will not gonna implode.

* the scripts are free to use and to modify. But please respect the copyright mentions inside the scripts.

(If YOU want to post scripts too, please add an disclaimer as mine too.
You could even use mine for non-working scripts)






Example Script [Not working]: Align Columns

based on idea by me and code by ' author    Scott Greenberg <me@gogogadgetscott.com>


This script will align CSV line by delimiter.
i.e: it will insert sapce on every line so all delimiters are on the same column.


Code (vb) Select

Const module_name  = "Align2StS"
Const module_ver   = "1.00b"

Sub AlignCol
    text = handleSelText("")
    Set regEx = New RegExp

    OldDelimiter = InputBox("Enter delimiter to identify columns." _
    & vbCrLf & "Use what you need like: blank, tab, ; , :", module_name, ",")
   
ExtraSpaceAmount = InputBox("Enter extra space amount to insert between columns.", module_name, "1")

NewDelimiter = InputBox("New delimiter:" _
& vbCrLf &  "As an default the old delimiter is set" _
& vbCrLf &  "Use an blank for non (delete the old delimiter) ", _
"New delimiter",OldDelimiter)


    regEx.Pattern    = "(.+)\s*" & OldDelimiter & "\s*(.*)"
    regEx.Global     = TRUE
    regEx.IgnoreCase = TRUE
    Set Matches = regEx.Execute(text)
    MaxLength = 0
    For Each Match in Matches
        length = len(Match.SubMatches(0))
        If length > MaxLength Then
            MaxLength = length
        End If
    Next
    newtext = ""

    For Each Match in Matches
        Spaces = MaxLength - Len(Match.SubMatches(0)) + ExtraSpaceAmount
        newtext = newtext & Match.SubMatches(0) & NewDelimiter & String(Spaces, " ") & Match.SubMatches(1)
    Next
    handleSelText newtext
End Sub

'// @param string Text to replace selected text
Private Function handleSelText(text)
    On Error Resume Next
    Set editor = newEditor()
    editor.assignActiveEditor
    If text = "" Then
        '// Get selected text
        handleSelText = editor.selText
        If handleSelText = "" Then
            '// No text was select. Get all text and select it.
            handleSelText  = editor.Text
            editor.command "ecSelectAll"
        End If
    Else
        '// Set selected text
        editor.selText text
    End If
End Function

Sub Init
    'menuName = "&" & module_name
    'addMenuItem "Align columns"         , menuName, "AlignCol"
    addMenuItem "Align columns Stefan", "Align Column", "AlignCol"
   
End Sub

Stefan, HippoEDIT beta tester 
HippoEDIT - the editor programmers wants to code thyself when they are dreaming.        -Don't just edit. HippoEDIT!-

Stefan

'// the last code from the original author Scott

' ------------------------------------------------------------------------------
' file      AlignColumns.vbs
' date      07/28/2005 - 02/12/2006
' author    Scott Greenberg <me@gogogadgetscott.com>
' link      http://gogogadgetscott.info/computers/cc/
' copyright Copyright (c) 2005-2006. SEG Technology. All rights reserved.
' requires  GoGo
' ------------------------------------------------------------------------------
Const module_name  = "AlignColumns"
Const module_ver   = "2.1"

Sub AlignCol
    Dim Columns(100)

    '// Get delimiter
    delimiter = InputBox("Enter delimiter.", module_name, ",")
    If delimiter = "" Then Exit Sub

        '// Get working text
        text = handleSelText("")

        '// Determine end-of-line
        EOL = ""
        If InStr(text, Chr(13)) Then
            EOL = EOL & Chr(13)
        End If
        If InStr(text, Chr(10)) Then
            EOL = EOL & Chr(10)
        End If

        '// Get lines
        lines = Split(text, EOL)

        '// Make delimiter regexp safe
        Set regReplace = New RegExp
        With regReplace
            .Global     = TRUE
            .IgnoreCase = TRUE
            .Pattern    = "([\$|\(|\)|\*|\+|\.|\[|\]|\?|\\|\^|\{|\}|\|])"
        End With
        safeDelimiter = regReplace.Replace(delimiter, "\$1")

        '// Setup regexp object
        Set regSearch = New RegExp
        With regSearch
            .Global     = TRUE
            .IgnoreCase = TRUE
            .Pattern    = "([^" & safeDelimiter & "]*)(\s*)" & safeDelimiter
        End With

        '// Get all column's max width
        For Each Line in lines
            Set Matches = regSearch.Execute(Line)
            '// Initialize column index
            Column = 0
            For Each Match in Matches
                Length = len(Match.SubMatches(0))
                If Length > Columns(Column) Then
                    Columns(Column) = Length
                End If
                Column = Column + 1
            Next
        Next

        '// Initialize line index
        i = -1

        '// Added spacing
        For Each Line in lines
            Column = 0
            newLine = ""
            i = i + 1
            Set Matches = regSearch.Execute(Line)

            '// Sum matches and remove from orignal line
            '// Match should catch everything before delimiter
            foundText = ""
            For Each Match in Matches
                foundText = foundText & Match.SubMatches(0) & Match.SubMatches(1) & delimiter
            Next
            extraText = Replace(Line, foundText, "")

            For Each Match in Matches
                Spaces  = Columns(Column) - Len(Match.SubMatches(0))
                newLine = newLine & Match.SubMatches(0) & String(Spaces, " ") & delimiter
                Column  = Column + 1
            Next
            If newLine <> "" Then
                lines(i) = newLine & extraText
            End If
        Next

        '// Replace text
        text = Join(lines, EOL)
        handleSelText text
    End Sub

    '// @param string Text to replace selected text
    Private Function handleSelText(text)
        On Error Resume Next
        Set editor = newEditor()
        editor.assignActiveEditor
        If text = "" Then
            '// Get selected text
            handleSelText = editor.selText
            If handleSelText = "" Then
                '// No text was select. Get all text and select it.
                handleSelText  = editor.Text
                editor.command "ecSelectAll"
            End If
        Else
            '// Set selected text
            editor.selText text
        End If
    End Function

    Sub Init
        addMenuItem "Align columns Scott", "Align Column", "AlignCol"
    End Sub
Stefan, HippoEDIT beta tester 
HippoEDIT - the editor programmers wants to code thyself when they are dreaming.        -Don't just edit. HippoEDIT!-