Sign in to follow this  
Followers 0

AU3 Syntax Highlight for RichEdit - Machine Code Version - Updated 12/25/2013


64 posts in this topic

Posted (edited)

Last Update - 12/25/2013
 
Here are a few functions that will take any Autoit code and convert it to RTF format with AutoIt syntax highlighted.
The code can be saved or applied right to the RichEdit control you are working with.

Special thanks go to MrCreator. A good amount of ideas for different parts of the code and regular expressions came from studying his >Autoit Syntax Highlight for HTMLBBCode. Viewing how he handled certain situations was very helpful. Thanks to Robjong also for feedback on some of the SRE expressions.

Update - 12/25/2013

Fixed incorrect coloring for INI functions

Update - 10/29/2013
Heres my latest version. Its been completly rewritten in assembly and is working very fast. The previous version for this library worked by checking for each catagory (variable, keyword, string, etc) one catagory at a time using reg expressions. The libaray works by walking the data one time adds the coloring each time it hits a catagory. Each time we hit a catogory, we jump to that catagoys procedure and continue processing until that catagory is over, then return to the main loop, or another catagorey. This make processing parts like comments and strings very fast. 
 
For strings Im passing the data to an autoit function for the send keys. For the UDFs im also passing the data to an auto it function, but only to check if its a function, not modify any data. Using a dictionary, I setup a structure where the key is the function name and the item for that key is the length. Checking to see if a key exists in a dictionary is very fast, so is the lookup so if it is a key, I pass back the length of the funtion, along with the function type (Native, UDF, Keyword). Code for Native functions and Keywords are built into the assembly code, the reason they are also in the udfs is for case insensitve checks. The assembly code then adds the coloring and copys the bytes specifed by the length.
 
There full assembly code is in the build folder. That includes everything you need compile the full function if you wanted to change it somehow. Theres also a short little brief on assembly code in general and working with strings in assembly. 
 
Update - 10/20/2013


Alot of the performance changes I made can really only be seen if you are using one of the new beta versions of autoit. This is mainly due to the regexp engine updates. If your still on 3.3.8.1 you wont see much speed changes, but with 3.3.9.18 and up, you should see a lot.
Check out these time comparisons for parsing WinApi.au3:
 
-3.3.9.21 (Beta)
ReplaceRichEditTags 	= 22.4785932416519
Mark Strings 		= 726.858602765271
Vars 			= 7.65824463155854
Operators 		= 132.610412680671
Numbers 		= 96.6795644227232
Macros 			= 11.6307915154246
ComObjects 		= 27.3305066622565
Special 		= 3.60985095820728
Functions 		= 2621.53934766058
Keywords 		= 407.872668637923
UDFs 			= 100.098942992298
PreProcessor 		= 37.0309118437898
Strings 		= 1002.14652117009
Comments		= 625.995489111994
CleanUp 		= 46.58760731893
RestoreCommentBlocks 	= 0.598790443304525
Total Time 		= 6017.28508413006

-3.3.8.1
ReplaceRichEditTags 	= 59.0612676678823
Mark Strings 		= 1487.37892314667
Vars 			= 21.9932878442689
Operators 		= 292.73325026304
Numbers 		= 191.066046585896
Macros 			= 27.8009848677101
ComObjects 		= 54.8651723518493
Special 		= 6.52339417234329
Functions 		= 4179.81049708161
Keywords 		= 644.222099929571
UDFs 			= 192.171242661253
PreProcessor 		= 64.9037515646964
Strings 		= 2307.7007302392
Comments 		= 6276.41364443823
CleanUp 		= 91.1678409612583
RestoreCommentBlocks 	= 0.897045111731445
Total Time 		= 16117.5066365941


 
Please let me know if you have any problems. Thanks
 

Snap%203.PNG


Change Log:


 
Update: 10/20/13
  • Updated to support new betas. RegEx has changed a bit and needed some modifications
  • Quite a few changes to how the quotes strings get parsed. 
  • added ternary operators
  • New Beta RegEx engine is alot faster! Really should get on if you haven't yet. 

Update: 3/18/12

  • Got more help from Robjong with some of the regular expressions. Thanks again!
  • Fixed crash when loading large file.

Update: 2/19/12

  • Added new function _RESH_SetColorTable() that allows the user to change the color table to there own preference.

Update: 2/18/12

  • Rewrote for speed. Also broke out colors and fonts from RTF header making them much easier to modify.


RESH.zip
RESHv2.zip
RESHv2.1.zip
RESHv2.2.zip
RESHv2.3.zip
 
asm version:
RESHv3.zip
RESHv3.1.zip

Edited by Beege
MikahS likes this

Share this post


Link to post
Share on other sites



Posted

Updated this... because its so popular. :)

Share this post


Link to post
Share on other sites

Posted

Very nice indeed. Have you thought about adopting the latest style? (If you don't ask you don't get!)

Share this post


Link to post
Share on other sites

Posted

Thanks Guinness.:) Yes I have thought about it. I've been wonder weather I should have preset styles that the user could switch, or create a function that allows them to set the colors to whatever they want. I think just passing an array of hex color values would be easy enough but wasnt sure which route I should go. What do you think?

Share this post


Link to post
Share on other sites

Posted

The regular expression use in this is nutz, awesome work!

Share this post


Link to post
Share on other sites

Posted

What do you think?

An array should be fine. You've also got me hooked on using associative arrays.

Share this post


Link to post
Share on other sites

Posted (edited)

The regular expression use in this is nutz, awesome work!

Thanks

An array should be fine.

Array it is then.

You've also got me hooked on using associative arrays.

Their addicting! It seemed the more I started using them the more situations started poping up that fit them perfectly! Plus their pretty fast! :) Edited by Beege

Share this post


Link to post
Share on other sites

Posted

Updated with new function to change color table to whatever you want.

Share this post


Link to post
Share on other sites

Posted

Updated with new function to change color table to whatever you want.

Sweet! :)

Share this post


Link to post
Share on other sites

Posted

Thanks!

Before I start writing something, do you have or know of any snippet that will extract the color values from the Scite User Options file?

Share this post


Link to post
Share on other sites

Posted

Beege

Before I start writing something, do you have or know of any snippet that will extract the color values from the Scite User Options file?

"Case $Import" in

([()[]<>.*+-=&^,/]) = ([][()<>.*+=&^,/-])

Share this post


Link to post
Share on other sites

Posted (edited)

Thanks AZJIO, I came up with the following:

#include <array.au3>

Global $aCurrentColors = _GetCurrentSciteColors()
_ArrayDisplay($aCurrentColors)

Func _GetCurrentSciteColors($sSciteUserPropPath = '')

	 If Not $sSciteUserPropPath Then $sSciteUserPropPath = @UserProfileDir & 'SciteUser.properties'
	 If Not FileExists($sSciteUserPropPath) Then Return SetError(1, 0, 0)

	 Local $sSciteUserProps = FileRead($sSciteUserPropPath)
	 If @error Then Return SetError(2,0,0)

	 Local $aUserColors = StringRegExp($sSciteUserProps, '(?mi)^style.au3.(?:d|10|11|12|13|14|15)=fore:(#[0-9a-f]{6}).*$', 3)
	 If @error Then Return SetError(3, @error, 0)

	 Return $aUserColors

EndFunc   ;==>_GetCurrentSciteColors

([()[]<>.*+-=&^,/]) = ([][()<>.*+=&^,/-])

Thats partly true but I think some of those characters would need need to be escaped. I would surely think '^' would because thats an operorator for sets.

Edit: Changed expression to more secure version. Thanks AZJIO

Edited by Beege

Share this post


Link to post
Share on other sites

Posted (edited)

screenshot

(?mi)^style.au3.(?:)|10|11|12|13|14|15)=fore:#([0-9a-f]{6}).*$

# au3
style.au3.0=fore:#72ADC0   # White space
style.au3.1=fore:#71AE71,italics   # Comment line
style.au3.2=fore:#71AE71,italics   # Comment block
style.au3.3=fore:#C738B9   # Number
style.au3.4=fore:#AAA6DB   # Function
style.au3.5=fore:#0080FF   # Keyword
style.au3.6=fore:#FF46FF   # Macro
style.au3.7=fore:#999999   # String
style.au3.8=fore:#FF8080   # Operator
style.au3.9=fore:#D29A6C   # Variable
style.au3.10=fore:#EA9515,bold   # Sent keys
style.au3.11=fore:#F000FF   # Pre-Processor
style.au3.12=fore:#0080C0   # Special
style.au3.13=fore:#7D8AE6,bold   # Abbrev-Expand
style.au3.14=fore:#0080FF,bold   # Com Objects
style.au3.15=fore:#72ADC0   # Standard UDF's
Edited by AZJIO

Share this post


Link to post
Share on other sites

Posted

Beege,

I tried your version of _GetCurrentSciteColors which returned only 2 results. Then I substituted your SRE for AZJIO's and it returned 15, which looking at au3.properties is correct. Nice function by the way, I'll be saving that to my functions folder.

Share this post


Link to post
Share on other sites

Posted

Yup that works too. Only I would need the # included for the new function.

(?mi)^style.au3.(?:)|10|11|12|13|14|15)=fore:(#[0-9a-f]{6}).*$

Share this post


Link to post
Share on other sites

Posted

Beege,

I tried your version of _GetCurrentSciteColors which returned only 2 results. Then I substituted your SRE for AZJIO's and it returned 15, which looking at au3.properties is correct. Nice function by the way, I'll be saving that to my functions folder.

Ya my version is a little two specific. AZJIO's version will be more secure for property files that might have been modified or slightly different in some way. Ill update my previous post.

Share this post


Link to post
Share on other sites

Posted

screenshot

Is that PCRE engine? I'm confused why the doc states:

 Note that special characters do not retain their special meanings inside a set, with the exception of , ^, -,[ and ] match
the escaped character inside a set.

Share this post


Link to post
Share on other sites

Posted (edited)

I was using the new version and suddenly BAM, right when it was about to load the large UDF into the edit control it gave me this.

Posted Image

I'm on a x86 acer aspire one d250 with win xp if its of any importance.

Edited by THAT1ANONYMOUSEDUDE

Share this post


Link to post
Share on other sites

Posted

The pic you posted is not visible. The code you were trying to highlight is the only thing that might help me figure out what happened..

Share this post


Link to post
Share on other sites

Posted (edited)

Hmm, really? the image isn't visible? damn, I need to find a better site then.

Anyway, the image doesn't matter, what matters is that AutoIt is crashing on my PC when loading anything above about 2k or 3k lines of script, and aside from that, on your 3rd version, It would be nice if we could just copy and past the result as in your second version.

Also, AutoIt is crashing no matter what version I use, but I haven't tried the first file you posted, just v2 and v2.1.

Must be something that has to do with running on a crappy laptop.

Edited by THAT1ANONYMOUSEDUDE

Share this post


Link to post
Share on other sites

Posted

Must be something that has to do with running on a crappy laptop.

Which is?

I tested on Windows 7 x64 AutoIt v3.3.8.1 without any crashing.

Share this post


Link to post
Share on other sites

Posted

Which is?

I tested on Windows 7 x64 AutoIt v3.3.8.1 without any crashing.

I really have no idea, this is only happening when I run it on my Acer Aspire One D250 and not on any other machine, all my versions of autoit are also v3.3.8.1.

Share this post


Link to post
Share on other sites

Posted

I really have no idea

You're probably running Windows XP from searching online.

Share this post


Link to post
Share on other sites

Posted

Anyway, the image doesn't matter, what matters is that AutoIt is crashing on my PC when loading anything above about 2k or 3k lines of script.

Sorry I miss read your first post. If its fails when you try and load then thats not my UDF. The load file function is just a few basic functions from the standard library. I would be curios which one it fail on. If you want you can debug a little bit by replacing the Load file function.

Func _LoadFile($sFile)
ConsoleWrite('>Error code: ' & @error & @CRLF & @CRLF & '@@ Trace(56) :	 Local $script = FileRead($sFile)' & @CRLF) ;### Trace Console
Local $script = FileRead($sFile)
ConsoleWrite('>Error code: ' & @error & @CRLF & @CRLF & '@@ Trace(58) :	 _GUICtrlRichEdit_SetSel($hRichEdit, 0, -1)' & @CRLF) ;### Trace Console
_GUICtrlRichEdit_SetSel($hRichEdit, 0, -1)
ConsoleWrite('>Error code: ' & @error & @CRLF & @CRLF & '@@ Trace(60) :	 _GUICtrlRichEdit_ReplaceText($hRichEdit, '''')' & @CRLF) ;### Trace Console
_GUICtrlRichEdit_ReplaceText($hRichEdit, '')
ConsoleWrite('>Error code: ' & @error & @CRLF & @CRLF & '@@ Trace(62) :	 _GUICtrlRichEdit_AppendText($hRichEdit, $script)' & @CRLF) ;### Trace Console
_GUICtrlRichEdit_AppendText($hRichEdit, $script)
ConsoleWrite('>Error code: ' & @error & @CRLF & @CRLF & '@@ Trace(64) :	 _GUICtrlRichEdit_GotoCharPos($hRichEdit, 0)' & @CRLF) ;### Trace Console
_GUICtrlRichEdit_GotoCharPos($hRichEdit, 0)
EndFunc   ;==>_LoadFile

and aside from that, on your 3rd version, It would be nice if we could just copy and past the result as in your second version.

I'm not sure what you mean here. My 3rd version is no different from the second except that I added the color table function. I never had any copy paste options because thats more of a user preference and not something that should just automatically happen. If you want it on the clipboard just use Clipput().

Share this post


Link to post
Share on other sites

Posted

Let me elaborate, my initial post was failure.

The problem is happening when the _GUICtrlRichEdit_AppendText is called.

Func _GUICtrlRichEdit_AppendText($hWnd, $sText)
If Not _WinAPI_IsClassName($hWnd, $_GRE_sRTFClassName) Then Return SetError(101, 0, False)
Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd)
_GUICtrlRichEdit_SetSel($hWnd, $iLength, $iLength) ; go to end of text
Local $tSetText = DllStructCreate($tagSETTEXTEX)
DllStructSetData($tSetText, 1, $ST_SELECTION)
Local $iRet
If StringLeft($sText, 5) <> "{rtf" And StringLeft($sText, 5) <> "{urtf" Then
  DllStructSetData($tSetText, 2, $CP_UNICODE)
  $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "wstr")
Else
  DllStructSetData($tSetText, 2, $CP_ACP)
  $iRet = _SendMessage($hWnd, $EM_SETTEXTEX, $tSetText, $sText, 0, "struct*", "STR") <------- And autoit dies when this function is called right here
EndIf
If Not $iRet Then Return SetError(700, 0, False)
Return True
EndFunc   ;==>_GUICtrlRichEdit_AppendText

So I guess it has something to do with the "SendMessageW" API and not "your" script at all.

I'd help more but I have to go now.

br

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0