NotepadUltra/Notepad Ultra/Find.vb

100 lines
4.6 KiB
VB.net

Public Class Find 'TODO: will changing file in npu while find is open fuck things? probably
Dim StartBounds, EndBounds As Integer
Dim FindOptions As RichTextBoxFinds
Public Property npu As NPUWindow
Private Sub FindStuff(sender As Object, e As EventArgs) Handles NextButton.Click, PreviousButton.Click
If EndBounds = 0 Then EndBounds = npu.TextFileHolder.TextLength
If sender.name = "PreviousButton" Then 'BACKWARDS
FindOptions = RichTextBoxFinds.Reverse Or GetFindSettings()
StartBounds = 0
EndBounds -= 1
Else 'FORWARDS
FindOptions = RichTextBoxFinds.None Or GetFindSettings()
EndBounds = npu.TextFileHolder.TextLength
StartBounds += 1
End If
Dim FindMeCountable As String() = New String(0) {FindMe.Text.ToLower}
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
Dim SelectPos As Integer = npu.TextFileHolder.SelectionStart
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(SelectPos, 0)
End Sub
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 Button2.Click
Close()
End Sub
Private Sub FindMe_TextChanged(sender As Object, e As EventArgs) Handles FindMe.TextChanged
HighlightAllButton.Enabled = FindMe.Text <> ""
PreviousButton.Enabled = FindMe.Text <> ""
NextButton.Enabled = FindMe.Text <> ""
End Sub
Private Function GetFindSettings()
Return 0 + (4 * Math.Abs(CInt(MatchCaseCheckBox.Checked))) + (2 * Math.Abs(CInt(WholeWordCheckBox.Checked)))
End Function
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