2012年8月12日日曜日

サクラエディタ用GrepDeleteStrategy


サクラエディタのGrep結果を基に行の削除を行うStrategyです。
実行するにはサクラエディタ用Grep結果処理マクロが必要です。
Class Strategy

    Private lngLSize
    Private lngFSize
    Private strPreviousPath

    'コンストラクタ
    Private Sub Class_Initialize()
        strPreviousPath = ""
        lngLSize = 0
        lngFSize = 0
    End Sub
    
    'デストラクタ
    Private Sub Class_Terminate()
    End Sub
    
    '開始処理
    Public Function execStartProc()
        execStartProc = False

        Dim objDialog: Set objDialog = CreateObject("WScript.Shell")
        Dim lngRtn: lngRtn = objDialog.Popup( _
            "Grep結果を基に行の削除を行います。" & vbCrLf & _
            "Grep結果以外のファイルは閉じてください。" & vbCrLf & _
            "またGrep結果はGrepReplace実行時に自動的に閉じられますので" & vbCrLf & _
            "結果を残したい場合は事前に保存をしてください。", _
            0, "GrepReplace", 1)
            
        If lngRtn = 1 Then
            execStartProc = True
        Else
            MsgBox "処理をキャンセルしました。"
        End If
        Set objDialog = Nothing
    End Function
    
    '前処理
    Public Function execPreProc(ByRef objGrepResults)
        execPreProc = True
    End Function
    
    '本処理
    Public Function execProc(ByRef objGrepResult)
        With objGrepResult
            If strPreviousPath <> .strFilePath Then
                If strPreviousPath <> "" Then
                    Call Editor.FileSave()
                    Call Editor.FileClose()
                End If
                Call Editor.FileOpen(.strFilePath)
                lngFSize = lngFSize + 1
            End If
            Call deleteLine(.strFilePath, .lngRow)
            lngLSize = lngLSize + 1
            strPreviousPath = .strFilePath
        End With
        execProc = True
    End Function

    '後処理
    Public Function execPostProc(ByRef objGrepResults)
        Call Editor.FileSave()
        Call Editor.FileClose()
        execPostProc = True
    End Function
    
    '終了処理
    Public Sub execEndProc()
        MsgBox lngFSize & "ファイル、合計" & lngLSize & "行削除しました。"
    End Sub
    
    'エラー処理
    Public Sub execErrProc(ByRef lngErrCode)
        If lngErrCode = ERR_FORMAT Then
            MsgBox "Grepファイルフォーマットエラー"
        Else
            MsgBox "エラー"
        End If
    End Sub
    
    Private Sub deleteLine(ByRef strFilePath, ByRef lngRow)
        With Editor
            Call .GoFileTop(0)
            Call .Jump(lngRow, 1)
            Call .DeleteLine()
            Call .ReDraw(0)
        End With
    End Sub

End Class

Call main(New Strategy)

0 件のコメント:

コメントを投稿