NotepadUltra/Notepad Ultra/FindReplace.vb

103 lines
4.7 KiB
VB.net

Public Class FindReplace
Dim StartBounds, EndBounds As Integer
Dim FindOptions As RichTextBoxFinds
Public Property npu As NPUWindow
Private Sub HighlightAllButton_Click(sender As Object, e As EventArgs) Handles HighlightAllButton.Click
'StartBounds = 0
'EndBounds = npu.TextFileHolder.TextLength
Dim LastFound As Integer = 0
Try
While True
npu.TextFileHolder.[Select](npu.TextFileHolder.Find(FindMe.Text, LastFound, npu.TextFileHolder.TextLength, RichTextBoxFinds.None), FindMe.Text.Length)
npu.TextFileHolder.SelectionFont = New Font(npu.TextFileHolder.Font, FontStyle.Bold)
npu.TextFileHolder.SelectionBackColor = Color.Yellow
LastFound = npu.TextFileHolder.Find(FindMe.Text, LastFound, npu.TextFileHolder.TextLength, RichTextBoxFinds.None) + 1
End While
Catch ex As Exception
Dim FindMeCountable As String() = New String(0) {FindMe.Text.ToLower}
FindStatus.Text = ("Found " & npu.TextFileHolder.Text.ToString.ToLower.Split(FindMeCountable, StringSplitOptions.None).Length - 1 & " results").Replace("1 results", "1 result")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles ViewNextButton.Click
'this code shares a lot with find.vb. in fact it pretty much IS just the find code from find.vb
If EndBounds = 0 Then EndBounds = npu.TextFileHolder.TextLength
If ForwardsRadio.Checked Then
FindOptions = RichTextBoxFinds.None Or GetFindSettings()
StartBounds += 1
EndBounds = npu.TextFileHolder.TextLength
Else
FindOptions = RichTextBoxFinds.Reverse Or GetFindSettings()
StartBounds = 0
EndBounds -= 1
End If
FindStatus.Text = ("Found " & CountFindMeAppearances() & " results").Replace("1 results", "1 result")
ResetFormatting()
If FindStatus.Text = "Found 0 results" Then
Beep()
Else
Dim findResult As Integer = npu.TextFileHolder.Find(FindMe.Text, StartBounds, EndBounds, FindOptions)
If findResult = -1 Then
FindStatus.Text = "Reached last instance, looping"
StartBounds = 0
EndBounds = npu.TextFileHolder.TextLength
End If
npu.TextFileHolder.[Select](npu.TextFileHolder.Find(FindMe.Text, StartBounds, EndBounds, FindOptions), FindMe.Text.Length)
npu.TextFileHolder.SelectionFont = New Font(npu.TextFileHolder.Font, FontStyle.Bold)
npu.TextFileHolder.SelectionBackColor = Color.Yellow
If StartBounds < 0 Then StartBounds = 0
If EndBounds < 0 Then EndBounds = npu.TextFileHolder.TextLength
StartBounds = npu.TextFileHolder.Find(FindMe.Text, StartBounds, EndBounds, FindOptions)
EndBounds = npu.TextFileHolder.Find(FindMe.Text, StartBounds, EndBounds, FindOptions) + FindMe.Text.Length
npu.TextFileHolder.SelectionStart = StartBounds
If sender.name = "PreviousButton" Then npu.TextFileHolder.SelectionStart = EndBounds
npu.TextFileHolder.ScrollToCaret() 'TODO: Fix jumpiness
End If
End Sub
Public Sub ResetFormatting() Handles Me.Closed
npu.TextFileHolder.[Select](0, npu.TextFileHolder.TextLength)
npu.TextFileHolder.SelectionFont = New Font(npu.TextFileHolder.Font, FontStyle.Regular)
npu.TextFileHolder.SelectionBackColor = npu.TextFileHolder.BackColor
npu.TextFileHolder.Select(0, 0)
End Sub
Private Function GetFindSettings()
Return 0 + (4 * Math.Abs(CInt(MatchCaseCheckBox.Checked))) + (2 * Math.Abs(CInt(WholeWordCheckBox.Checked)))
End Function
Private Sub ReplaceOnceButton_Click(sender As Object, e As EventArgs) Handles ReplaceOnceButton.Click
If npu.TextFileHolder.SelectedText <> FindMe.Text Then ViewNextButton.PerformClick() '~u0
If FindStatus.Text <> "Found 0 results" Then npu.TextFileHolder.SelectedText = ReplaceWithMe.Text
ViewNextButton.PerformClick()
End Sub
Private Sub ReplaceAllButton_Click(sender As Object, e As EventArgs) Handles ReplaceAllButton.Click
npu.TextFileHolder.Text = npu.TextFileHolder.Text.Replace(FindMe.Text, ReplaceWithMe.Text) 'TODO: make it work better than this
ResetFormatting()
End Sub
Public Function CountFindMeAppearances()
Dim FindMeCountable As String() = New String(0) {FindMe.Text.ToLower}
Select Case GetFindSettings()
Case 0
Return npu.TextFileHolder.Text.ToString.ToLower.Split(FindMeCountable, StringSplitOptions.None).Length - 1
Case 2 'WHOLE WORD
Return CountWholeWordFindMeAppearances(FindMe.Text, FindOptions, False)
Case 4 'MATCH CASE
FindMeCountable = New String(0) {FindMe.Text}
Return npu.TextFileHolder.Text.ToString.Split(FindMeCountable, StringSplitOptions.None).Length - 1
Case 6 'ALL OPTIONS
Return CountWholeWordFindMeAppearances(FindMe.Text, FindOptions, True)
Case Else
Return npu.TextFileHolder.Text.ToString.ToLower.Split(FindMeCountable, StringSplitOptions.None).Length - 1
End Select
End Function
End Class