サクラエディタのGrep結果を基に置換を行うStrategyです。
実行するにはサクラエディタ用Grep結果処理マクロが必要です。
Const OPT_WORD = 1 '単語単位で探す
Const OPT_DIFF = 2 '英大文字と小文字を区別する
Const OPT_RE = 4 '正規表現
Const OPT_MSG = 8 '見つからないときにメッセージを表示
Const OPT_DCLS = 16 '検索ダイアログを自動的に閉じる
Const OPT_LOOP = 32 '先頭(末尾)から再検索する
Const OPT_CB = 64 'クリップボードから貼り付ける
Const OPT_TGT = 128 '設定なし:ファイル全体、設定あり:選択範囲
Class Strategy
Private lngLSize
Private lngFSize
Private strBefore
Private strAfter
Private lngReplaceOption
Private strPreviousPath
'コンストラクタ
Private Sub Class_Initialize()
strPreviousPath = ""
lngReplaceOption = OPT_TGT + OPT_DCLS + OPT_DIFF
lngLSize = 0
lngFSize = 0
End Sub
'デストラクタ
Private Sub Class_Terminate()
End Sub
'開始処理
Public Function execStartProc()
execStartProc = False
strBefore = InputBox("置換前の文字列を入力してください。")
If strBefore = "" Then
MsgBox "処理をキャンセルしました。"
Exit Function
End If
strAfter = InputBox("置換後の文字列を入力してください。")
Dim objDialog: Set objDialog = CreateObject("WScript.Shell")
Dim lngRtn: lngRtn = objDialog.Popup( _
"Grep結果を基に下記の置換を行います。" & vbCrLf & _
"置換前='" & strBefore & "'" & vbCrLf & _
"置換後='" & strAfter & "'" & 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 replaceLine(.strFilePath, .lngRow, strBefore, strAfter)
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 "'" & strBefore & "'→'" & strAfter & "'" & vbCrLf & _
lngFSize & "ファイル、合計" & lngLSize & "行置換しました。"
End Sub
'エラー処理
Public Sub execErrProc(ByRef lngErrCode)
If lngErrCode = ERR_FORMAT Then
MsgBox "Grepファイルフォーマットエラー"
Else
MsgBox "エラー"
End If
End Sub
Private Sub replaceLine(ByRef strFilePath, ByRef lngRow, ByRef strBfr, ByRef strAft)
With Editor
Call .GoFileTop(0)
Call .Jump(lngRow, 1)
Call .GoLineTop(1)
Call .BeginSelect()
Call .GoLineEnd_Sel()
Call .ReplaceAll(strBfr, strAft, lngReplaceOption)
Call .ReDraw(0)
End With
End Sub
End Class
Call main(New Strategy)
0 件のコメント:
コメントを投稿