Syntax Highlighting Routine - Need help

Garrett Hylltun garrett at paraboliclogic.com
Thu Mar 23 02:08:27 EST 2006


Rev 2.6.1

Greetings,

I made a routine for checking a FIELD from start to end for html tags, 
then to change their color.  Worked great!  That is with a small html 
file of maybe 4000 characters.  But then I ran a html file that was 
about 28000 characters, and found my routine was not so good after all.

the FIELD handlers;

on rawKeyUp
   SubHighlight
end rawKeyUp

on backspaceKey
   SubHighlight
end backspaceKey

might not be best, but I don't know if there's any other way to keep 
track of the contents of the FIELD, except to check on keyup events.

Here's the sub;

on SubHighlight
   lock screen
   put number of characters of field "fHtmlEditor" into varTotal
   set the textstyle of char 1 to varTotal of field 1 to plain
   put 1 into varCounter
   put 1 into varStartChunk
   put 1 into varEndChunk
   repeat until varStartChunk >= varTotal or varGetOut is 1
     put varCounter into field "fStatusBar"
     put char varEndChunk to varEndChunk of field 1 into varTemp
     put empty into varTagType
     if varTemp is "<" then
       put varEndChunk + 3 into varTemp
       put char varEndChunk to varTemp of field 1 into varTemp
       if varTemp is "<!--" then
         put "comment" into varTagType
       end if
       put varEndChunk + 6 into varTemp
       put char varEndChunk to varTemp of field 1 into varTemp
       if varTemp is "<script" then
         put "script" into varTagType
       end if
       put varEndChunk + 7 into varTemp
       put char varEndChunk to varTemp of field 1 into varTemp
       if varTemp is "</script" then
         put "script" into varTagType
       end if
       put varEndChunk + 1 into varEndChunk
       put char varEndChunk to varEndChunk of field 1 into varTemp
       repeat until varTemp is ">" or varGetOut is 1 or varCounter >= 
varTotal
         put varCounter into field "fStatusBar"
         put char varEndChunk to varEndChunk of field 1 into varTemp
         if varTemp is "<" then
           put 1 into varGetOut
         end if
         if varTemp is ">" then
           if varTagType is "comment" then
             set the textstyle of char varStartChunk to varEndChunk of 
field 1 to Italic
             set the foregroundColor of char varStartChunk to 
varEndChunk of field 1 to "#404040"
           else if varTagType is "script" then
             set the foregroundColor of char varStartChunk to 
varEndChunk of field 1 to "#ff0000"
           else
             --set the textstyle of char varStartChunk to varEndChunk of 
field 1 to Bold
             set the foregroundColor of char varStartChunk to 
varEndChunk of field 1 to "#0000FF"
           end if
         end if
         put varEndChunk + 1 into varEndChunk
         put varCounter + 1 into varCounter
         --put varTotal && "/" && varCounter into field "fStatusBar"
       end repeat
     else
       put varStartChunk + 1 into varStartChunk
       put varEndChunk + 1 into varEndChunk
     end if
     put varCounter + 1 into varCounter
     put varEndChunk into varStartChunk
     --put varTotal && "/" && varCounter into field "fStatusBar"
   end repeat
   unlock screen
end SubHighlight

It's not optimized code and there's some extra stuff in there for 
testing.  And I know it's not pretty code either, it's merely a few 
hours worth of thinking and trying code.

I do not know of any other way to check the contents of the FIELD 
without having to check the entire contents.

Does anyone have any ideas, thoughts, suggestions or have done some code 
like this before, that could help me out with this?

Thanks,
-Garrett



More information about the use-livecode mailing list