Sign in to follow this  
Followers 0
darkshadow791

Custom Syntax Highlighting with GuiCtrlCreateEdit() ?

7 posts in this topic

#1 ·  Posted (edited)

I have seen this:

http://www.autoitscript.com/forum/index.ph...36&hl=dttvb

But the script is a bit too advanced for me, and I don't really understand how to use it for personal needs.

What I'm trying to do is create my own set of syntax highlighting (like scite's) but instead of AutoIt's syntax I want to be able to create my own, and color code, etc.

However, I only want the highlighting to be a visual thing - I want the user to be able to save their script as plain text (.cfg .rc .txt etc) without any syntax highlighting stuff inside the file, but when they open the file it is highlighted.

Also, if possible, it would be nice if I could implement the function-prompter-thingy (like if you type in the beginning of the function it will list what it could be, or if you type in a variable a list of variables drops down). However, this seems much more complicated then just syntax highlighting, and I don't want it if it causes problems.

So my question is, how can I add syntax highlighting to the Edit field in the following script, and still be able to save as plain text.

Here is the script so far (I haven't tried to add in any syntax highlighting yet, this is just the plain script)

_linenums:0'>
#compiler_icon = C:\Program Files\AutoIt3\Icons\filetype-blank.ico

#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <Date.au3>
#include <File.au3>

Global $random = String( (Chr(Random(Asc("a"), Asc("z"), 1))) & Random(0, 9, 1) & Chr(Random(Asc("a"), Asc("z"), 1)) & Random(0, 9, 1) & "_" )
Global $CustRand = 0

$GUI = GuiCreate("Script Randomizer", 400, 300,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Edit_1 = GuiCtrlCreateEdit("//Write your script here!" & @CRLF, 0, 0, 400, 300)

$M_File = GUICtrlCreateMenu("File")
$M_New = GUICtrlCreateMenuitem("New", $M_File)
$M_Open = GUICtrlCreateMenuitem("Open", $M_File)
$M_Save = GUICtrlCreateMenuitem("Save", $M_File)
$M_Exit = GUICtrlCreateMenuitem("Exit", $M_File)
GUICtrlCreateMenuitem("", $M_File)
$M_Mini = GUICtrlCreateMenuitem("Randomize A File", $M_File)

$M_Edit = GUICtrlCreateMenu("Edit")

$B_RandMenu = GUICtrlCreateMenu("Random", $M_Edit)
$B_Rand = GUICtrlCreateMenuitem("Randomize!", $B_RandMenu)
$B_SetRand = GUICtrlCreateMenuitem("Set A Custom Random", $B_RandMenu)
$B_ResetRand = GUICtrlCreateMenuitem("Use Default Random", $B_RandMenu)
GUICtrlCreateMenuitem("", $M_Edit)
$M_Undo = GUICtrlCreateMenuitem("Undo", $M_Edit)
$M_Cut = GUICtrlCreateMenuitem("Cut", $M_Edit)
$M_Copy = GUICtrlCreateMenuitem("Copy", $M_Edit)
$M_Paste = GUICtrlCreateMenuitem("Paste", $M_Edit)
GUICtrlCreateMenuitem("", $M_Edit)
$M_Find = GUICtrlCreateMenuitem("Find", $M_Edit)
$M_Replace = GUICtrlCreateMenuitem("Replace", $M_Edit)
GUICtrlCreateMenuitem("", $M_Edit)
$M_SelectAll = GUICtrlCreateMenuitem("Select All", $M_Edit)
GUICtrlCreateMenuitem("", $M_Edit)
$M_TimeDate = GUICtrlCreateMenuitem("Time/Date", $M_Edit)

$M_Help = GUICtrlCreateMenu("Help")
$M_HelpOpen = GUICtrlCreateMenuitem("Help", $M_Help)
$M_About = GUICtrlCreateMenuitem("About", $M_Help)

$hlfol = RegRead("HKEY_CURRENT_USER\Software\Valve\Steam", "ModInstallPath")
If $hlfol = "" Then $hlfol = @DesktopCommonDir

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            _Exit()
        Case $B_Rand
            Randomize()
        Case $M_Copy
            Sleep(100)
            Send("^{c}")
        Case $M_Cut
            Sleep(100)
            Send("^{x}")
        Case $M_Exit
            _Exit()
        Case $M_Find
            _GuiCtrlEditFind($GUI, $Edit_1)
        Case $M_Replace
            _GuiCtrlEditFind($GUI, $Edit_1, True)
        Case $M_About
            About()
        Case $M_HelpOpen
            Help()
        Case $M_New
            New()
        Case $M_Open
            $open_file = FileOpenDialog("Open a file...",$hlfol, "All (*.*)|Text Files (*.txt;*.cfg;*.ini;*.log;*.rc)", 1)
            If Not $open_file = "" Then
                GUICtrlSetData($Edit_1, FileRead($open_file))
            Else
                MsgBox(0,"Error!","Error! File either does not exist or is blank!")
            EndIf
        Case $M_Paste
            Sleep(100)
            Send("^{v}")
        Case $M_Save
            Save()
        Case $M_SelectAll
            _GUICtrlEditSetSel($Edit_1, 0, -1)
        Case $M_TimeDate
            Send(_Now(), 1)
        Case $M_Undo
            _GUICtrlEditUndo($Edit_1)
        Case $M_Mini
            Mini()
        Case $B_SetRand
            CustomRandom()
        Case $B_ResetRand
            ResetRandom()
        Case Else
            ;;;
    EndSwitch
WEnd
Exit


Func Randomize()
    $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
    Dim $newscript[$script[0]]
    For $i = 0 To $script[0] Step 1
        If StringInStr($script[$i],"alias") Then
            If $CustRand == 1 Then
                
            Else
                Global $random = String( (Chr(Random(Asc("a"), Asc("z"), 1))) & Random(0, 9, 1) & Chr(Random(Asc("a"), Asc("z"), 1)) & Random(0, 9, 1) & "_" )
            EndIf
            $replace = StringSplit($script[$i]," ")
            Local $s_ReadEditData
            $s_ReadEditData = GUICtrlRead($Edit_1)
            If StringInStr($replace[2], $random) Then
                
            Else
                If StringInStr($replace[2],"_") Then
                    
                Else
                    If StringInStr($replace[2],'"') Then
                        $s_searchstring = StringTrimRight(StringTrimLeft($replace[2],1),1)
                        If StringInStr($replace[2],"+") Or StringInStr($replace[2],"-") Then
                            $s_searchstring = StringTrimLeft($s_searchstring, 1)
                            $s_replacestring = String($random & $s_searchstring)
                            GUICtrlSetData($Edit_1,StringReplace($s_ReadEditData,$s_searchstring,$s_replacestring))
                            $replace = StringSplit($script[$i]," ")
                            $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
                        Else
                            $s_replacestring = String($random & $s_searchstring)
                            GUICtrlSetData($Edit_1,StringReplace($s_ReadEditData,$s_searchstring,$s_replacestring))
                            $replace = StringSplit($script[$i]," ")
                            $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
                        EndIf
                    Else
                        $s_searchstring = $replace[2]
                        If StringInStr($replace[2],"+") Or StringInStr($replace[2],"-") Then
                            $s_searchstring = StringTrimLeft($s_searchstring, 1)
                            $s_replacestring = String($random & $s_searchstring)
                            GUICtrlSetData($Edit_1,StringReplace($s_ReadEditData,$s_searchstring,$s_replacestring))
                            $replace = StringSplit($script[$i]," ")
                            $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
                        Else
                            $s_replacestring = String($random & $s_searchstring)
                            GUICtrlSetData($Edit_1,StringReplace($s_ReadEditData,$s_searchstring,$s_replacestring))
                            $replace = StringSplit($script[$i]," ")
                            $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    Next
EndFunc

Func New()
    $savefirst = MsgBox(3, "Save first?", "Would you like to save first?")
    If $savefirst = 2 Then
        Return 0
    ElseIf $savefirst = 6 Then
        Save()
        Sleep(100)
        GUICtrlSetData($Edit_1, "")
        Return 1
    ElseIf $savefirst = 7 Then
        GUICtrlSetData($Edit_1, "")
        Return 2
    EndIf
EndFunc

Func Save()
    $save_file = FileSaveDialog("Save a file...",$hlfol,"All (*.*)", 16, "MyScript.cfg")
    If $save_file == "" Then
        $savesure = MsgBox(4,"Error!","Invalid save! Do you wish to save?")
        If $savesure == 6 Then
            Save()
        EndIf
    EndIf
    $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
    _FileWriteFromArray($save_file, $script, 1)
EndFunc

Func CheckCustom()
    $script = StringSplit(GUICtrlRead($Edit_1),Chr(10))
    For $i = 0 To $script[0] Step 1
        If StringInStr($script[$i],"%r") Then
            Global $Custom = 1
            Return True
            ExitLoop
        EndIf
    Next
    Return False
EndFunc

Func About()
    MsgBox(0,"Random Scripter v1.0","Random Scripter v1.0" & @CRLF & "Made by DarkShadow" & @CRLF & @CRLF & @CRLF & "Visit www.vacdisabled.net")
EndFunc

Func Help()
    MsgBox(0,"Help","This works a lot like Notepad." & @CRLF & "Just type in your script and when you are done," & @CRLF & "Go to edit>Randomize" & @CRLF & "And all your aliases will be randomized!" & @CRLF & @CRLF & @CRLF & @CRLF & "NOTE!: This is a beta version" & @CRLF & "There may be bugs" & @CRLF & "Also, I don't suggest randomizing your script after" & @CRLF & "you have already randomized it")
EndFunc

Func _Exit()
    $savefirst = MsgBox(3, "Save first?", "Would you like to save first?")
    If $savefirst = 2 Then
        
    ElseIf $savefirst = 6 Then
        $save = Save()
        If Not $save == 1 Then
            Sleep(100)
            Exit
        EndIf
    ElseIf $savefirst = 7 Then
        Exit
    EndIf
EndFunc

Func Pause()
    Sleep(10)
EndFunc

Func Mini()
    $mini_file = FileOpenDialog("Open a script to randomize", $hlfol, "All files (*.*)", 1)
    If $mini_file == "" Then
        MsgBox(0, "Error!", "Error! Unable to open file!")
        Return 0
    EndIf
    $mini_confirm = MsgBox(4, "Confirm", "Are you sure you wish to randomize this file?" & @CRLF & "Once randomized, a backup will be made named " & $mini_file & ".Backup")
    If $mini_confirm == 6 Then
        $saveme = GUICtrlRead($Edit_1)
        GUICtrlSetData($Edit_1, "")
        GUICtrlSetData($Edit_1, FileRead($mini_file))
        $mini_save = StringSplit(GUICtrlRead($Edit_1),Chr(10))
        $mini_backup = $mini_file & ".BACKUP"
        _FileWriteFromArray($mini_backup, $mini_save, 1)
        Randomize()
        $mini_newsave = StringSplit(GUICtrlRead($Edit_1),Chr(10))
        _FileWriteFromArray($mini_file, $mini_newsave, 1)
        Sleep(1)
        GUICtrlSetData($Edit_1, $saveme)
        MsgBox(0,"Success!","File has successfully been randomized!")
    Else
        Return 0
    EndIf
EndFunc

Func ResetRandom()
    Global $random = String( (Chr(Random(Asc("a"), Asc("z"), 1))) & Random(0, 9, 1) & Chr(Random(Asc("a"), Asc("z"), 1)) & Random(0, 9, 1) & "_" )
    Global $CustRand = 0
EndFunc

Func CustomRandom()
    $C_Rand = InputBox("Customize the random","Choose your custom random code")
    If StringInStr($C_Rand, "_") Then
        Global $random = $C_Rand
    Else
        Global $random = $C_Rand & "_"
    EndIf
    Global $CustRand = 1
EndFunc

Edit: Updated code.

Edited by darkshadow791

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