Function FixURL(Optional $txtBody)
; This regex fixes pasted URL's so they are converted back to the way they are displayed in HTML
; Richard.von.Mallesch@csiro.au <mailto:Richard.von.Mallesch@csiro.au> -> Richard.von.Mallesch@csiro.au
; www.csiro.au <http://www.csiro.au/> -> www.csiro.au
; https://www.csiro.au:443/foo/bar.htm <https://www.csiro.au:443/foo/bar.htm> -> https://www.csiro.au:443/foo/bar.htm
; <\\server1-syd\share$\file.text> -> \\server1-syd\share$\file.text
; It also checks for URLS and formats them as such.
Dim $KeyCode, $Paste, $regEx, $regEx2, $ss, $match, $diff, $text, $matched
If VarType($txtBody)
$Paste = 1
Else
$txtBody = $System.Sender
$KeyCode = $txtBody.KeyCode
EndIf
$ss = $txtBody.SelectionStart
$text = Replace($txtBody.Text, @CRLF, Chr(10))
If Len($text) = $ss
$text = $text + " "
Else
$text = Left($text, $ss + 1)
EndIf
If $KeyCode = 32 Or $KeyCode = 13 Or ($KeyCode = 86 And $txtBody.LastKey = 17) Or $Paste ;Space, Enter, Ctrl+V, or pasted
$regEx = CreateObject("VBscript.RegExp")
$regEx.Pattern = "<?((\\\\[\w-.\\$]+)|((([\w.%+-]+@)|(\w{3,6}:(//)?)|(\w+\.))\w+(\.\w+)+(:\d+)?[^\s>]*))>?( <\w{0,6}:?(//)?\1/?>)?"
$regEx.Global = 1
$regEx2 = CreateObject("VBscript.RegExp")
$regEx2.Pattern = "^\w+(\.\w+){2,}$"
If $KeyCode = 32 Or $KeyCode = 13 $txtBody.LastSelectionStart = InStrRev(Replace(Left($text, -2), Chr(10), " "), " ") EndIf
For Each $match in $regEx.Execute(SubStr($text, $txtBody.LastSelectionStart + 1, $ss - $txtBody.LastSelectionStart))
If Not ($match.Value = $match.SubMatches(0) And $RegEx2.Test($match.Value))
$txtBody.SelectionStart = $txtBody.LastSelectionStart + $match.FirstIndex + $diff
$txtBody.SelectionLength = $match.Length
$txtBody.SelectionColor = "Blue"
$txtBody.SelectionFontUnderline = 1
$txtBody.SelectedText = $match.SubMatches(0)
$diff = $diff + Len($match.SubMatches(0)) - $match.Length
EndIf
$matched = 1
Next
If $matched And $ss + $diff = $txtBody.SelectionStart + $txtBody.SelectionLength
$txtBody.SelectionStart = $ss + $diff
$txtBody.SelectionColor = $txtBody.LastColour
$txtBody.SelectionFontUnderLine = 0
$txtBody.SelectedText = $txtBody.SelectedText + " "
$txtBody.SelectionStart = $ss + $diff + 1
Else
$txtBody.SelectionStart = $ss + $diff
EndIf
Else
If Right($text, 1) = " " And $KeyCode = 8 ; BackSpace
$regEx = CreateObject("VBScript.RegExp")
$regEx.Pattern = "((\\\\[\w-.\\$]+)|((([\w.%+-]+@)|(\w{3,6}:(//)?)|(\w+\.))\w+(\.\w+)+(:\d+)?[^\s>]*))"
If $regEx.Test($txtBody.LastWord) And $txtBody.SelectionFontUnderLine = 1
$txtBody.SelectionStart = $txtBody.LastSelectionStart - Len($txtBody.LastWord)
$txtBody.SelectionLength = Len($txtBody.LastWord) - 1
$txtBody.SelectedText = $txtBody.LastWord
$txtBody.SelectionColor = $txtBody.LastColour
$txtBody.SelectionStart = $txtBody.LastSelectionStart
$text = $text + " "
EndIf
Else
$txtBody.LastKey = $KeyCode
$txtBody.LastColour = $txtBody.SelectionColor
EndIf
EndIf
$text = Left($text, -1)
$ = InStrRev(Replace($text, Chr(10), " "), " ")
$txtBody.LastWord = SubStr($text, IIf($, $, 1))
$txtBody.LastSelectionStart = $txtBody.SelectionStart
EndFunction