October 28, 2020, 05:41:50 AM

Author Topic: [bb] Sibly's Syntax Highlighting by Kuron [ 1+ years ago ]  (Read 1444 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Sibly's Syntax Highlighting by Kuron [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Sibly's Syntax Highlighting
Author : Kuron
Posted : 1+ years ago

Description : I went hunting for this, and thought I would stick it in the code archives so it is easy for people to find.  Mark originally posted this in the forums on May 11, 2003.

Code :
Code: BlitzBasic
  1. ;
  2. ;Demo of a simple syntax highlighting editor!
  3. ;
  4. ;Auto-capitalizes words and sets some funky colors for numbers,
  5. ;strings and comments.
  6. ;
  7. ;Formatting is performed 'per-line', and changes in linecount are
  8. ;tracked to determine how many lines to reformat.
  9. ;
  10. ;Note: the color and formatting logic is a little convoluted in
  11. ;the interests of minimizing textarea modifications for SPEED.
  12. ;
  13.  
  14. window=CreateWindow( "Simple syntax highlighting text editor",0,0,640,480 )
  15.  
  16. textarea=CreateTextArea( 0,0,ClientWidth(window),ClientHeight(window)-96,window )
  17. SetGadgetLayout textarea, 1,1,1,1
  18.  
  19. font=LoadFont( "lucida",20 )
  20. SetTextAreaFont textarea,font
  21. SetTextAreaColor textarea,0,0,0,True
  22. SetTextAreaColor textarea,255,255,255
  23.  
  24. ActivateGadget textarea
  25.  
  26. Global output=CreateTextArea( 0,ClientHeight(window)-96,ClientWidth(window),96,window )
  27. DisableGadget output
  28. SetGadgetLayout output,1,1,0,1
  29.  
  30. SetTextAreaColor output,0,0,0
  31. SetTextAreaColor output,192,192,192,True
  32.  
  33. bp$=GetEnv$("blitzpath")
  34. tmp$=bp$+"/tmp/tmp.bb"
  35. bcc$=bp$+"/bin/blitzcc"
  36.  
  37. Info( "Output window active!" )
  38. Info( "blitzpath="+bp$ )
  39.  
  40. main_menu=WindowMenu( window )
  41. file_menu=CreateMenu( "File",0,main_menu )
  42. edit_menu=CreateMenu( "Edit",0,main_menu )
  43. prog_menu=CreateMenu( "Program",0,main_menu )
  44. go_menu=CreateMenu( "Go!",301,prog_menu )
  45.  
  46. UpdateWindowMenu window
  47.  
  48. HotKeyEvent 63,0,$1001,301;F5=program->go menu item
  49. HotKeyEvent 1,0,$803,0,0,0,0,window;ESC=close window!
  50.  
  51. n_lines=TextAreaLen( textarea,2 )
  52.  
  53. While WaitEvent()<>$803
  54.  
  55. Select EventID()
  56. Case $103;key hit
  57. Notify "Bam!"
  58. Case $1001;menu action
  59. Select EventData()
  60. Case 301
  61. t_file=WriteFile( tmp$ )
  62. If t_file
  63. WriteLine t_file,TextAreaText$( textarea )
  64. CloseFile t_file
  65. proc=CreateProcess( bcc$+" "+tmp$ )
  66. If proc
  67. While Not Eof(proc)
  68. ln$=ReadLine$(proc)
  69. If Len(ln$) Info( ln$ )
  70. Wend
  71. Else
  72. Info( "Error creating blitzcc process",True )
  73. EndIf
  74. Else
  75. Info( "Error writing tmp file",True )
  76. EndIf
  77. End Select
  78. Case $401;gadget action
  79. Select EventSource()
  80. Case textarea
  81. ;calc row and column
  82. row=TextAreaCursor( textarea,2 )
  83. col=TextAreaCursor( textarea,1 )-TextAreaChar( textarea,row )
  84. t_lines=0
  85. If EventData()=1;modified?
  86.  
  87. ;work how many lines added/removed
  88. n=TextAreaLen( textarea,2 )
  89. t_lines=n-n_lines
  90. n_lines=n
  91. If t_lines<0 t_lines=0
  92.  
  93. ;lock textareas before big changes
  94. LockTextArea textarea
  95.  
  96. ;format the lines!!!!!
  97. For n=row-t_lines To row
  98.  
  99. FormatLine( textarea,n )
  100.  
  101. Next
  102.  
  103. ;unlock textarea
  104. UnlockTextArea textarea
  105.  
  106. EndIf
  107. SetStatusText window,"row="+(row+1)+" col="+(col+1)+" lines="+n_lines+" chars="+TextAreaLen(textarea)+" formated="+(t_lines+1)
  108. End Select
  109. End Select
  110. Wend
  111.  
  112. End
  113.  
  114. Function info( t$,err=False )
  115. If err t$="Error! - "+t$
  116. AddTextAreaText output,t$+Chr$(10)
  117. End Function
  118.  
  119. ;return true for digits
  120. Function IsDigit( i$ )
  121. t=Asc(i$)
  122. Return t>=48 And t<58
  123. End Function
  124.  
  125. ;return true for alphabetic characters
  126. Function IsAlpha( i$ )
  127. t=Asc(i$)
  128. Return (t>=65 And t<65+26) Or (t>=97 And t<97+26)
  129. End Function
  130.  
  131. Function FormatLine( textarea,n )
  132.  
  133. ;line we're formatting
  134. ln$=TextAreaText( textarea,n,1,2 )
  135.  
  136. ;start-of-line character index
  137. sol=TextAreaChar( textarea,n )
  138.  
  139. ;color and format info
  140. fmt_ch=0:fmt_r=0:fmt_g=0:fmt_b=0:fmt_f=-1
  141.  
  142. ;next char to process
  143. ch=1
  144.  
  145. While ch<=Len(ln$)
  146.  
  147. ;save char index and get next char
  148. t_ch=ch
  149. i$=Mid$(ln$,ch,1)
  150.  
  151. ;turn off formating flags (bold etc) by default
  152. f=0
  153.  
  154. If IsDigit(i$)
  155. ;Format a numeric value
  156. Repeat
  157. ch=ch+1
  158. If ch>Len(ln$) Exit
  159. i$=Mid$(ln$,ch,1)
  160. Until Not IsDigit(i$)
  161. r=255:g=0:b=192;barf red
  162. Else If IsAlpha(i$)
  163. ;Format an identifier
  164. z$=i$
  165. q$=Upper$(i$)
  166. Repeat
  167. ch=ch+1
  168. If ch>Len(ln$) Exit
  169. i$=Mid$(ln$,ch,1)
  170. z$=z$+i$
  171. q$=q$+Lower$(i$)
  172. Until Not IsAlpha(i$)
  173. If q$<>z$
  174. SetTextAreaText textarea,q$,sol+t_ch-1,Len(q$)
  175. EndIf
  176. r=255:g=255:b=0:f=1;yellow, bold
  177. Else If i$=Chr$(34)
  178. ;Format a string
  179. Repeat
  180. ch=ch+1
  181. If ch>Len(ln$) Exit
  182. i$=Mid$(ln$,ch,1)
  183. Until i$=Chr$(34)
  184. If i$=Chr$(34) ch=ch+1
  185. r=0:g=255:b=0
  186. Else If i$=";"
  187. ;Format a comment
  188. ch=Len(ln$)+1
  189. r=0:g=128:b=255:f=3;blue, bold-italic
  190. Else
  191. ;Default formatting
  192. ch=ch+1
  193. r=255:g=255:b=255;white
  194. EndIf
  195.  
  196. ;if format changed, apply previous
  197. If r<>fmt_r Or g<>fmt_g Or b<>fmt_b Or f<>fmt_f
  198. If fmt_ch
  199. FormatTextAreaText textarea,fmt_r,fmt_g,fmt_b,fmt_f,sol+fmt_ch-1,t_ch-fmt_ch
  200. EndIf
  201. fmt_ch=t_ch:fmt_r=r:fmt_g=g:fmt_b=b:fmt_f=f
  202. EndIf
  203.  
  204. Wend
  205.  
  206. ;format to EOL
  207. If fmt_ch
  208. FormatTextAreaText textarea,fmt_r,fmt_g,fmt_b,fmt_f,sol+fmt_ch-1,ch-fmt_ch
  209. EndIf
  210.  
  211. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal