Author Topic: Example Script [Not working]: Selecting portion of text  (Read 2252 times)

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
Example Script [Not working]: Selecting portion of text
« on: September 27, 2009, 08:26:14 PM »
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]: Selecting portion of text


This script is a big mess... but it works for PSPad.
I always want to put some code into an function.... but this is still undone.


The purpose of this script is to select some lines by different rules.


The code:

The message exceeds the maximum allowed length (20000 characters). So see below next post:
Stefan, HippoEDIT beta tester 
HippoEDIT - the editor programmers wants to code thyself when they are dreaming.        -Don't just edit. HippoEDIT!-

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
Re: Example Script [Not working]: Selecting portion of text
« Reply #1 on: September 27, 2009, 08:28:04 PM »
Code: [Select]
'ToDo: goto x line --- select one to much
' if vHelp = 1 MsgBox ,,"Info: go to  a line",,"Info"
'// VBScript for PSPad Editor  www.PSPad.com    --- Y = line , X = column
'// Purpose: Goto and select the text from here till there. Go to your start line and start this script.
'// Stefan , 07-2007. Based on an script "select.vbs" from gogogadgetscott, Date: 07/27/2005
Const module_name  = "GoToAndSelectEnhanced2007"
Const module_ver   = "1.000"
Const myAppName = "GoTo and select"
Const vHelpMsgBox = 1    'use a 0 digit to disable the help pop-us ==> 'vHelpMsgBox = 0'
'##########################################################

Sub Init
    menuName = "&" & "Go to... and select"
    addMenuItem "Go up      and select x lines"                  , menuName, "GoToUpAndSel"   ',"Ctrl+Shift+Alt+U"
    addMenuItem "Go up      to line number x and select lines"   , menuName, "GoToUpLineNoSel"
    addMenuItem "Go up      and select x lines as column"        , menuName, "GoToUpSelCol"
   'addMenuItem "Go up      to bookmark 0 and select lines"      , menuName, "GoUpToBM0Sel"
    addMenuItem "Go up      to bookmark X and select lines"      , menuName, "GoUpToBMSel"
    addMenuItem "Go up      to bookmark X and select column"     , menuName, "GoUpToBMSelCol"
    addMenuItem "Go up      to find and select lines"            , menuName, "GoToUpFindSel"
    addMenuItem "Go down  and select x lines"                    , menuName, "GoToDownAndSel" ',"Ctrl+Shift+ALt+D"
    addMenuItem "Go down  to line number x and select lines"     , menuName, "GoToDownLineNoSel"
    addMenuItem "Go down  and select x lines as column"          , menuName, "GoToDownSelCol"
   'addMenuItem "Go down  to bookmark 0 and select lines"        , menuName, "GoDownToBM0Sel"
    addMenuItem "Go down  to bookmark X and select lines"        , menuName, "GoDownToBMSel"
    addMenuItem "Go down  to bookmark X and select column"       , menuName, "GoDownToBMSelCol"
    addMenuItem "Go down  to find and select lines"              , menuName, "GoToDownFindSel"
    addMenuItem "&Open this script file to edit"                 , menuName, "openThisScriptFile"
End Sub
'#########################################################


'// Purpose: Go to bookmark 0 and select between.
'// Optionally ask the user for the bookmark number with an InputBox.
'// This script works with hole lines only.
'// Use: Put cursor in your start line, then start the script and answer the question.
'// Stefan , 07-2007. Based on an script "select.vbs" from gogogadgetscott, Date: 07/27/2005
Sub GoUpToBM0Sel
    vCommand = "ecGotoMarker0"
    GoUpToBMSeldoit(vCommand)
End Sub

Sub GoUpToBMSel
    vCommand = "ecGotoMarker0"
    'Default bookmark is Marker No.0, if the user should be able to choose an other bookmark, uncomment the next 2 lines to ask him:
    vUserInput = InputBox("Enter bookmark to select up:", module_name, "0")
    vCommand = "ecGotoMarker" & vUserInput
    GoUpToBMSeldoit(vCommand)
End Sub

Private Function GoUpToBMSeldoit(vCommand)
    Set editor = newEditor()
    With editor
        .assignActiveEditor
        .command "ecLineSelect"
        vPos1 = .caretY                   'remember your current position
              'MsgBox "vPos1: " & vPos1   ' line 20 for ex
        .command vCommand                 '"ecGotoMarker0"          'goto bookmark
        vPos2 = .caretY                   'store this position where the bookmark is
              'MsgBox "vPos2: " & vPos2   ' line 5 for ex
        vGoLinesAmount = vPos1 - vPos2
              'MsgBox "Lines between: " & vGoLinesAmount &" Counted from: "& vPos1 &" - "& vPos2   '20-5=15
        .caretY(vPos1)                    'go back to where you came from
'go and do the selection:
        For i = vGoLinesAmount to 1 Step -1
                    .command "ecSelUp"
        Next
     End With
    vCommand=""
    vPos1=""
    vPos2=""
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Function

'===========================================================

Sub GoDownToBMoSel
    vCommand = "ecGotoMarker0"
    GoDownToBMSeldoit(vCommand)
End Sub

Sub GoDownToBMSel
    vCommand = "ecGotoMarker0"
    'Default bookmark is Marker No.0, if the user should be able to choose an other bookmark, uncomment the next 2 lines to ask him:
    vUserInput = InputBox("Enter bookmark to select down:", module_name, "0")
    vCommand = "ecGotoMarker" & vUserInput
    GoDownToBMSeldoit(vCommand)
End Sub

Function GoDownToBMSeldoit(vCommand)
    Set editor = newEditor()
    With editor
        .assignActiveEditor
        .command "ecLineSelect"
        vPos1 = .caretY                   'remember your current position
              'MsgBox "vPos1: " & vPos1   ' line 3 for ex
        .command vCommand                 '"ecGotoMarker0"          'goto bookmark
        vPos2 = .caretY                   'store this position where the bookmark is
              'MsgBox "vPos2: " & vPos2   ' line 10 for ex
        vGoLinesAmount = vPos2 - vPos1
              'MsgBox "Lines between: " & vGoLinesAmount &" Counted from: "& vPos2 &" - "& vPos1   '10-3=7
        .caretY(vPos1)                    'go back to where you came from
'go and do the selection:
        For i = vGoLinesAmount to 1 Step -1
                    .command "ecSelDown"
       Next
    End With
    vCommand=""
    vPos1=""
    vPos2=""
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Function
'######################################################


'// Purpose: Go to bookmark 0 and select a column between.
'// Optionally ask the user for the bookmark number with an InputBox.
'// This script works with column only.
'// Use: Put cursor in your start line, select some text, then start the script and answer the question.
'// You can also use this to go to the same column position in an other, far away line.
'// Stefan , 07-2007. Based on an script "select.vbs" from gogogadgetscott, Date: 07/27/2005


'neu'

Sub GoUpToBM0SelCol
    vCommand = "ecGotoMarker0"
    GoUpToBMSelColdoit(vCommand)
End Sub

Sub GoUpToBMSelCol
'msgbox "entered sub up sel col"
    vCommand = "ecGotoMarker0"
    'Default bookmark is Marker No.0, if the user should be able to choose an other bookmark, uncomment the next 2 lines to ask him:
    vUserInput = InputBox("Enter bookmark to select up:" _
    & vbCRLF & "Note: you have to select some text from LEFT-to-RIGHT!" _
    & vbCRLF & "Use this function without an selection to go to the same column in an other line only.", module_name, "0")
    vCommand = "ecGotoMarker" & vUserInput
    GoUpToBMSelColdoit(vCommand)
End Sub

Private Function GoUpToBMSelColdoit(vCommand)
'msgbox "entered Private func up sel col"
    Set editor = newEditor()
    With editor
        .assignActiveEditor
        .command "ecColumnSelect"
        'remember your current position
        vPosy1 = .caretY
        vPosx1 = .caretX
        vSelLen = Len(.selText())
        'msgbox " vSel: " & vSel
        'MsgBox "vPosy1: " & vPosy1  & "  vPosx1: " & vPosx1 ' line 20 for ex
        
        '"ecGotoMarker0"          'goto bookmark
        .command vCommand
        'store this position where the bookmark is
        vPosy2 = .caretY
        'MsgBox "vPosy2: " & vPosy2   ' line 5 for ex
        'go back to where you came from
        .caretY(vPosy1)
        .caretX(vPosx1)
        'MsgBox "I am back at vPos1: " & vPos1  & "  vPos1 X: " & vPos1X ' i am back
        're-select
       For i = 1 to vSelLen
        .command "ecSelLeft"
        Next      
        vGoLinesAmount = vPosy1 - vPosy2
        'MsgBox "Lines between: " & vGoLinesAmount &" Counted from: "& vPosy1 &" - "& vPosy2   '20-5=15

        'go and do the column selection:
        For i = vGoLinesAmount to 1 Step -1
                    .command "ecSelUp"
        Next
     End With
    vCommand=""
    vPosy1=""
    vPosx1=""
    vPosy2=""
    vSelLen=""
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Function
'===========================================================

Sub GoDownToBMoSelCol
    vCommand = "ecGotoMarker0"
    GoDownToBMSelColdoit(vCommand)
End Sub

Sub GoDownToBMSelCol
    vCommand = "ecGotoMarker0"
    'Default bookmark is Marker No.0, if the user should be able to choose an other bookmark, uncomment the next 2 lines to ask him:
    vUserInput = InputBox("Enter bookmark to select down:", module_name, "0")
    vCommand = "ecGotoMarker" & vUserInput
    GoDownToBMSelColdoit(vCommand)
End Sub

Function GoDownToBMSelColdoit(vCommand)
    Set editor = newEditor()
    With editor
      .assignActiveEditor
        .command "ecColumnSelect"
        'remember your current position
        vPosy1 = .caretY
        vPosx1 = .caretX
        vSelLen = Len(.selText())
        'MsgBox " vSel: " & vSelLen
        'MsgBox "vPosy1: " & vPosy1  & "  vPosx1: " & vPosx1 ' line 5 for ex
        
        '"ecGotoMarker0"          'goto bookmark
        .command vCommand
        'store this position where the bookmark is
        vPosy2 = .caretY
        'MsgBox "vPosy2: " & vPosy2   ' line 20 for ex
        'go back to where you came from
        .caretY(vPosy1)
        .caretX(vPosx1)
        'MsgBox "I am back at vPosy1: " & vPosy1  & "  vPosx1: " & vPosx1 ' i am back
        're-select
       For i = 1 to vSelLen
        .command "ecSelLeft"
        Next      
        vGoLinesAmount = vPosy2 - vPosy1
        'MsgBox "Lines between: " & vGoLinesAmount &" Counted from: "& vPosy2 &" - "& vPosy1   '20-5=15

        'go and do the column selection:
        For i = vGoLinesAmount to 1 Step -1
                    .command "ecSelDown"
        Next
     End With

    vCommand=""
    vPosy1=""
    vPosx1=""
    vPosy2=""
    vSelLen=""
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Function

'#########################################################


'// Purpose: Ask the user for the amount of lines and select this amount of lines.
'// This script works with hole lines only.
'// Use: Put cursor in your start line, then start the script and answer the question.
'// Stefan , 05-2007. Based on an script "select.vbs" from gogogadgetscott, Date: 07/27/2005
Sub GoToUpAndSel
    Set editor = newEditor()
    vUserInput = InputBox("Enter lines to select up:" _
                 & vbCrLf & "(leave blank to select till top of file)", module_name, "")
    With editor
        .assignActiveEditor
        .command "ecDown"
        .command "ecLineStart"
        .command "ecSelLineStart"
            If vUserInput = "" Then
               vGoLinesAmount = .caretY
            ELSE
              vGoLinesAmount = vUserInput
            End If
            
            For i = vGoLinesAmount to 1 Step -1
                  .command "ecSelUp"                
            Next
                        
    End With
vUserInput=""
vGoLinesAmount=""
Set editor = Nothing
End Sub
'================================================================
  join this with the code from the next post

The message exceeds the maximum allowed length (20000 characters).


Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
Re: Example Script [Not working]: Selecting portion of text
« Reply #2 on: September 27, 2009, 08:28:19 PM »
The rest of code from last post:
Code: [Select]
Sub GoToDownAndSel
    Set editor = newEditor()
    vUserInput = InputBox("Enter lines to select down:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")

    With editor
        .assignActiveEditor
        .command "ecLineStart"
        .command "ecSelLineEnd"
              If vUserInput = "" Then
                 vGoLinesAmount = .linesCount - .caretY
              ELSE
                 vGoLinesAmount = vUserInput -1
              End If
              For i = vGoLinesAmount to 1 Step -1
                  .command "ecSelDown"
              Next
                  .command "ecSelLineEnd"
    End With
vUserInput=""
vGoLinesAmount=""
Set editor = Nothing
End Sub

'#######################################################


'// Purpose: Ask the user for the amount of lines and select this amount of lines.
'// This script works with column only.
'// Use: Select part of line in your start line with your column width, then start the script and answer the question.
'// Stefan , 05-2007. Based on an script "select.vbs" from gogogadgetscott, Date: 07/27/2005
Sub GoToUpSelCol
    Set editor = newEditor()
    vUserInput = InputBox("Enter lines to select up:" _
                 & vbCrLf & "(leave blank to select till top of file)", module_name, "")
    With editor
        .assignActiveEditor
        .command "ecColumnSelect"
            If vUserInput = "" Then
               vGoLinesAmount = .caretY -1
            ELSE
              vGoLinesAmount = vUserInput
            End If
        For i = vGoLinesAmount to 1 Step -1
            .command "ecSelUp"
        Next
    End With
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Sub
'=============================================================

Sub GoToDownSelCol
    Set editor = newEditor()
    vUserInput = InputBox("Enter lines to select down:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")
    With editor
        .assignActiveEditor
                ' IF editor.selText = "" Then
                ' MsgBox "nothing selected"
                ' End If
        .command "ecColumnSelect"
              If vUserInput = "" Then
                 vGoLinesAmount = .linesCount - .caretY
              ELSE
                 vGoLinesAmount = vUserInput -1
              End If
        For i = vGoLinesAmount to 1 Step -1
            .command "ecSelDown"
        Next
    End With
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
End Sub
'##########################################################

Sub GoToDownFindSel
    Set editor = newEditor()
    vUserInput = InputBox("Enter word to find and to select to:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")
      'Pattern = InputBox("Find: ", module_name, "Pspad")
    aPos = getStrPos(vUserInput, FALSE, FALSE, TRUE)
    If IsArray(aPos) Then
        '// Found a match, now move caret to match.
        Set editor = newEditor()
    With editor
        .assignActiveEditor
.command "ecLineSelect"
'remember your current position
        vPosy1 = .caretY
        vPosx1 = .caretX
        'vSelLen = Len(.selText())
        'MSGBOX vPosy1
        'set pos of found text
        .setCaretPos aPos(0), aPos(1)
        vPosy2 = .caretY
        vPosx2 = .caretX
        'MSGBOX vPosy2
       
        'go back to where you came from
        .caretY(vPosy1)
        .caretX(vPosx1)
        '// Select match
        'For i = 1 To Len(vUserInput)
         '   .command("ecSelRight")
        'Next
       
                ' IF editor.selText = "" Then
                ' MsgBox "nothing selected"
                ' End If
        vGoLinesAmount = vPosy2 - vPosy1
        'MSGBOX vPosy2 & " -  " & vPosy1 & " is " & vGoLinesAmount
        'vGoLinesAmount = vUserInput -1
        For i = vGoLinesAmount to 1 Step -1
            .command "ecSelDown"
        Next
    End With
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
   
    cleaner 'call the clean up function
   
    Else
        MsgBox "No match found: " & vUserInput, 0, myAppName
    End If
End Sub
'==================================================================

Sub GoToUpFindSel
    Set editor = newEditor()
    vUserInput = InputBox("Enter word to find above and to select to:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")
      'Pattern = InputBox("Find: ", module_name, "Pspad")
    aPos = getStrPos(vUserInput, FALSE, FALSE, False)
    If IsArray(aPos) Then
        '// Found a match, now move caret to match.
        Set editor = newEditor()
    With editor
        .assignActiveEditor
.command "ecLineSelect"
'remember your current position
        vPosy1 = .caretY
        vPosx1 = .caretX
        'MSGBOX vPosy1
        'set pos of found text
        .setCaretPos aPos(0), aPos(1)
        vPosy2 = .caretY
        vPosx2 = .caretX
        'MSGBOX vPosy2
       
        'go back to where you came from
        .caretY(vPosy1)
        .caretX(vPosx1)
        '// Select match
'         For i = 1 To Len(vUserInput)
'             .command("ecSelRight")
'         Next
       
                ' IF editor.selText = "" Then
                ' MsgBox "nothing selected"
                ' End If
        vGoLinesAmount = vPosy1 - vPosy2
        'MSGBOX vPosy2 & " -  " & vPosy1 & " is " & vGoLinesAmount
        'vGoLinesAmount = vUserInput -1
        For i = vGoLinesAmount to 1 Step -1
            .command "ecSelUp"
        Next
    End With
    vUserInput=""
    vGoLinesAmount=""
    Set editor = Nothing
   
    cleaner 'call the clean up function
   
    Else
        MsgBox "No match found: " & vUserInput, 0, myAppName
    End If
End Sub

'########################################################

Function getStrPos(Pattern, startCaretY, startCaretX, searchForward)
    Set editor = newEditor()
    editor.assignActiveEditor

    '// Get all text
    allText  = editor.Text
    lines = Split(allText, Chr(13))

    '// Set default return value
    getStrPos = FALSE

    '// Set caret positions
    If startCaretY = FALSE Then
        startCaretY = editor.caretY
    End If
    If startCaretX = FALSE Then
        startCaretX = editor.caretX
    End If
    If searchForward = TRUE Then
        endCaretY = UBound(lines)
        iStep = 1
    Else
        endCaretY = 1
        iStep = -1
    End If

    If startCaretY = endCaretY Then
        '// Nothing to search
        Exit Function
    End If

    For currCaretY = startCaretY To endCaretY Step iStep
        Line = Replace(lines(currCaretY - 1), Chr(10), "")
        If searchForward = TRUE Then
            currCaretX = InStr(startCaretX, Line, Pattern, 1)
            If currCaretY = startCaretY Then
                startCaretX = 1
            End If
        Else
            currCaretX = InStrRev(Line, Pattern, startCaretX, 1)
            If currCaretY = startCaretY Then
                startCaretX = -1
            End If
        End If
        If currCaretX > 0 Then
            '// We found a match
            GetStrPos = array(currCaretX, currCaretY)
            Exit For
        End If
    Next
End Function
'########################################################

Sub GoToUpLineNoSel
    Set editor = newEditor()
            With editor
        .assignActiveEditor
        vlinesCount = .linesCount
    vUserInput = InputBox("Enter line (max: " &  .caretY & " from "  & vlinesCount & " total)to go up and to select to:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")
    IF vUserInput <> "" Then 'todo'
        .command "ecLineSelect"
        'remember your current position
        vPosy1 = .caretY
        vGoLinesAmount = vPosy1 - vUserInput
        'MSGBOX vPosy2 & " -  " & vPosy1 & " is " & vGoLinesAmount
              For i = vGoLinesAmount to 1 Step -1
                  .command "ecSelUp"
              Next
      End If
        End With
        vUserInput=""
        vGoLinesAmount=""
    Set editor = Nothing
End Sub
'===============================================================

Sub GoToDownLineNoSel
    Set editor = newEditor()
        With editor
        .assignActiveEditor
    vlinesCount = .linesCount
    vUserInput = InputBox("Enter line (max: " & vlinesCount - .caretY & " from "  & vlinesCount & " total) to go down and to select to:" _
                 &  vbCrLf & "(leave blank to select till end of file)", module_name, "")
    IF vUserInput <> "" Then 'todo'
        .command "ecLineSelect"
        'remember your current position
        vPosy1 = .caretY
        vGoLinesAmount = vUserInput - vPosy1
        'MSGBOX vPosy2 & " -  " & vPosy1 & " is " & vGoLinesAmount
              For i = vGoLinesAmount to 1 Step -1
                  .command "ecSelDown"
              Next
    End If
        End With
        vUserInput=""
        vGoLinesAmount=""
    Set editor = Nothing
End Sub
'######################################################

'last actions before end the script
Sub Cleaner
'MsgBox "Cleaner loaded" 'function test

'set the selection mode to normal
Set editor = newEditor()
        editor.assignActiveEditor
        editor.command "ecNormalSelect"
Set editor = Nothing ' END set the selection mode to normal

'set the vars to nothing
        vUserInput=""
        vGoLinesAmount=""
End Sub 'Cleaner
'#########################################################

Sub openThisScriptFile
    Set obj1 = newEditor()
    obj1.openFile(moduleFileName(module_name))
End Sub