Syntax hilight example

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
diakin
Posts: 102
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Syntax hilight example

Post by diakin »

Hi, All!
I try to create my own syntax hilight example using Freebasic and RapidQ basic.

This is RapidQ example. Freebasic dll example is in the next post.

Code: Select all

'Based on "Very simple notepad like editor, in less than 200 lines of code! (by William Yu)" 
'parser based on  RQB2HTML - Rapid-Q BASIC source code to HTML converter by William Yu
'Syntax hilight based on example from Iczelion's Win32 Assembly Tutorial part 35
'http://spiff.tripnet.se/~iczelion/tut35.html
'Andrew Shelkovenko  diakin.narod.ru
'May 2004-2024

'Not complete, for example only.
'known parser bugs - with esc sequences, with tabs (for fix tabs replaced by spaces)
'colors choose is not completed.
' not fast, but better then SelStart\SelText.

'$APPTYPE   GUI
$INCLUDE   "RAPIDQ.INC"    '' You don't need them all in this example.

' Background Modes
#define TRANSPARENT  1
#define OPAQUE  2
#define BKMODE_LAST  2

'-- messages
CONST WM_DESTORY = 2
CONST WM_PAINT = &HF

'-- Edit Control Messages
const EM_EXLIMITTEXT = 1077
Public Const EM_GETSEL = &HB0
Public Const EM_SETSEL = &HB1
Public Const EM_GETRECT = &HB2
Public Const EM_SETRECT = &HB3
Public Const EM_SETRECTNP = &HB4
Public Const EM_SCROLL = &HB5
Public Const EM_LINESCROLL = &HB6
Public Const EM_SCROLLCARET = &HB7
Public Const EM_GETMODIFY = &HB8
Public Const EM_SETMODIFY = &HB9
Public Const EM_GETLINECOUNT = &HBA
Public Const EM_LINEINDEX = &HBB
Public Const EM_SETHANDLE = &HBC
Public Const EM_GETHANDLE = &HBD
Public Const EM_GETTHUMB = &HBE
Public Const EM_LINELENGTH = &HC1
Public Const EM_REPLACESEL = &HC2
Public Const EM_GETLINE = &HC4
Public Const EM_LIMITTEXT = &HC5
Public Const EM_CANUNDO = &HC6
Public Const EM_UNDO = &HC7
Public Const EM_FMTLINES = &HC8
Public Const EM_LINEFROMCHAR = &HC9
Public Const EM_SETTABSTOPS = &HCB
Public Const EM_SETPASSWORDCHAR = &HCC
Public Const EM_EMPTYUNDOBUFFER = &HCD
Public Const EM_GETFIRSTVISIBLELINE = &HCE
Public Const EM_SETREADONLY = &HCF
Public Const EM_SETWORDBREAKPROC = &HD0
Public Const EM_GETWORDBREAKPROC = &HD1
Public Const EM_GETPASSWORDCHAR = &HD2
Const EM_CHARFROMPOS = &HD7
Const EM_POSFROMCHAR = &HD6

'-- EDITWORDBREAKPROC code values
Public Const WB_LEFT = 0
Public Const WB_RIGHT = 1
Public Const WB_ISDELIMITER = 2

Type Point
Left As Long
Top As Long
End Type

CONST GWL_WNDPROC = (-4)
CONST GWL_HWNDPARENT = (-8)


declare Function ConvertCodePage(SourceString As String, inPage As long, outPage As long) As String

declare sub DecodUTF8sub ()

declare sub hiliteTxt

Declare Function TabbedTextOut Lib "user32" Alias "TabbedTextOutA" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, _
ByVal nCount As Long, ByVal nTabPositions As Long, lpnTabStopPositions As Long, ByVal nTabOrigin As Long) As Long

Declare Function SetBkColor Lib "gdi32" Alias "SetBkColor" (ByVal hdc As Long, ByVal crColor As Long) As Long
Declare Function GetBkColor Lib "gdi32" Alias "GetBkColor" (ByVal hdc As Long) As Long

DECLARE FUNCTION SetWindowLongAPI1 LIB "user32" ALIAS "SetWindowLongA" (ByVal hWnd AS LONG, ByVal nIndex AS LONG, ByVal dwNewLong AS LONG) AS LONG

Private Declare Function HideCaret Lib "user32" ALIAS "HideCaret" (ByVal hwnd As Long) As Long
Private Declare Function ShowCaret Lib "user32" ALIAS  "ShowCaret" (ByVal hwnd As Long) As Long

Declare Function CallWindowProc1 Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

DECLARE FUNCTION SendMessageApi LIB "user32.dll" ALIAS "SendMessageA" (hWnd AS LONG, Msg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG

Declare Function GetDC Lib "user32" Alias "GetDC"(ByVal hWnd As Long) As Long
Declare Function SetBkMode Lib "gdi32" Alias "SetBkMode" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long

Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function SetTextColor Lib "gdi32" Alias "SetTextColor" (ByVal hdc As Long, ByVal crColor As Long) As Long

'!!! ------------------------ SrcEditWndProc ------------------------------
declare FUNCTION SrcEditWndProc (hWnd AS LONG, uMsg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG

declare sub  R2Change



dim VisRect as QRect
dim VisPoint as Point
dim VisPoint1 as Point

dim HiLiteFont as QFont
dim HiLiteFont1 as QFont
HiLiteFont.bold=0
HiLiteFont.Name="FixedSys"
HiLiteFont.Name="DejaVu Sans Mono"


defint hdc '-- device handle'

DIM M AS QMEMORYSTREAM
defint FIRSTVISIBLELINE,LastVISIBLELINE

defint FirstChar



'---------------------------------------------'

create MainForm AS QForm
	
	create SrcEdit AS QRichEdit
		WantTabs=1
		
	end create
	
	create LogEdit AS QRichEdit
		align=albottom
		height=100
	end create
	
end create
create  DialogBox AS QForm
	create EditBox AS QEdit
	end create
	
	
end create


DIM MainMenu AS QMainMenu
DIM File AS QMenuItem, Edit AS QMenuItem
DIM New AS QMenuItem, Open AS QMenuItem, ReOpen AS QMenuItem
DIM ReOpen1 AS QMenuItem, ReOpen2 AS QMenuItem, ReOpen3 AS QMenuItem
DIM Save AS QMenuItem, SaveAs AS QmenuItem
DIM Break1 AS QMenuItem
DIM ExitEditor AS QMenuItem
DIM Copy AS QMenuItem, Cut AS QMenuItem, Paste AS QMenuItem, SelectAll AS QMenuItem
DIM Search AS QMenuItem, Find AS QMenuItem
DIM DecodUTF8 AS QMenuItem


DIM FileName AS STRING
FileName = "UNTiTLED"

DIM CountFiles AS BYTE
CountFiles = 0

DIM StatusBar AS QStatusBar
StatusBar.Parent = MainForm
StatusBar.AddPanels "0","1","2","3"
StatusBar.Panel(0).Width = 100
StatusBar.Panel(1).Width = 100
StatusBar.Panel(2).Width = 100
StatusBar.Panel(3).Width = 100
StatusBar.Panel(0).Alignment = taCenter



'--*********************************
SUB ExitEditorClick    '' Exit item clicked
MainForm.Close
END SUB

'--*********************************
SUB NewClick           '' New item clicked
SrcEdit.Clear
FileName = "UNTiTLED"
END SUB

'--*********************************
SUB OpenClick          '' Open item clicked
DIM OpenDialog AS QOpenDialog
OpenDialog.InitialDir=curdir$
OpenDialog.Filter ="1.All Files *.*|*.*|_
2.Bas Files  *.bas;*.rqb;*.vbs|*.bas;*.rqb;*.vbs|"
OpenDialog.FilterIndex = 1 


IF OpenDialog.Execute THEN
	FileName = OpenDialog.FileName
	'FileName = "C:\_F\bas\rapidq\RQIDE\Projects\sketchbook\ili9486.txt"
	SrcEdit.LoadFromFile FileName
	SrcEdit.text=REPLACESUBSTR$(SrcEdit.text,chr$(9),"    ") '-- заменяем табуляции пробелами'
	
	IF CountFiles = 0 THEN
		ReOpen.Enabled = True
		ReOpen1.Caption = "&1. "+FileName
		ReOpen.Insert 0, ReOpen1
	ELSEIF CountFiles = 1 THEN
		ReOpen2.Caption = "&2. "+FileName
		ReOpen.Insert 1, ReOpen2
	ELSEIF CountFiles = 2 THEN
		ReOpen3.Caption = "&3. "+FileName
		ReOpen.Insert 2, ReOpen3
	ELSE
		CountFiles = CountFiles - 1
		ReOpen1.Caption = "&1. "+RIGHT$(ReOpen2.Caption, LEN(ReOpen2.Caption)-4)
		ReOpen2.Caption = "&2. "+RIGHT$(ReOpen3.Caption, LEN(ReOpen3.Caption)-4)
		ReOpen3.Caption = "&3. "+FileName
	END IF
	CountFiles = CountFiles + 1
	SrcEdit.Modified = False
END IF
END SUB

'--*********************************
SUB SaveAsClick          '' SaveAs item clicked
DIM SaveDialog AS QSaveDialog

IF SaveDialog.Execute THEN
	SrcEdit.SaveToFile SaveDialog.FileName
END IF
END SUB

'--*********************************
SUB SaveClick
IF FileName = "UNTiTLED" THEN
	SaveAsClick
ELSE
	SrcEdit.SaveToFile FileName
END IF
END SUB

SUB CopyClick:  SrcEdit.CopyToClipBoard:    END SUB   '' Oh yeah, that was easy
SUB CutClick:   SrcEdit.CutToClipBoard:     END SUB
SUB PasteClick: SrcEdit.PasteFromClipBoard: END SUB
SUB SelectAllClick: SrcEdit.SelectAll:      END SUB

'--*********************************
SUB Reopen1Click
IF SrcEdit.Modified THEN
	SHOWMESSAGE "Abandoning changes"
END IF
SrcEdit.LoadFromFile RIGHT$(ReOpen1.Caption, LEN(ReOpen1.Caption)-4)
SrcEdit.Modified = False
END SUB

'--*********************************
SUB Reopen2Click
IF SrcEdit.Modified THEN
	SHOWMESSAGE "Abandoning changes"
END IF
SrcEdit.LoadFromFile RIGHT$(ReOpen2.Caption, LEN(ReOpen2.Caption)-4)
SrcEdit.Modified = False
END SUB

'--*********************************
SUB Reopen3Click
IF SrcEdit.Modified THEN
	SHOWMESSAGE "Abandoning changes"
END IF
SrcEdit.LoadFromFile RIGHT$(ReOpen3.Caption, LEN(ReOpen3.Caption)-4)
SrcEdit.Modified = False
END SUB

'--*********************************
SUB FindText   '' Primitive search...
SrcEdit.SelStart = INSTR(UCASE$(SrcEdit.Text), UCASE$(EditBox.Text))-1
IF SrcEdit.SelStart > 0 THEN
	DIM Font AS QFont
	Font.AddStyles(fsBold)
	Font.color=clRed
	SrcEdit.SelLength = LEN(EditBox.Text)
	SrcEdit.SelAttributes = Font
	DialogBox.Close
ELSE
	SHOWMESSAGE EditBox.Text+" not found."
END IF
END SUB

'--*********************************
SUB FindClick
DIM Button AS QButton

Button.Parent = DialogBox
Button.OnClick = FindText
Button.Caption = "&OK"
Button.Top = 13
Button.Left = 210
Button.Width = 70

EditBox.Parent = DialogBox
EditBox.Top = 15
EditBox.Left = 20
EditBox.Width = 170

DialogBox.BorderStyle = bsDialog
DialogBox.Caption = "Search..."
DialogBox.Width = 300
DialogBox.Height = 80
DialogBox.Center

DialogBox.ShowModal
END SUB

'--*********************************
SUB FormClose (Action AS INTEGER)
Application.Terminate
END SUB

'--*********************************
SUB FormResize
SrcEdit.Width = MainForm.ClientWidth
SrcEdit.Height = MainForm.ClientHeight
END SUB

'--*********************************
SUB RichShowXY  (X%, Y%, Shift%)
'StatusBar.Panel(0).Caption ="Row:"+ STR$(SrcEdit.WhereY+1)+ " Col:"+ STR$(SrcEdit.WhereX+1)
R2Change

END SUB

New.Caption = "&New"           : New.OnClick = NewClick
Open.Caption = "&Open"         : Open.OnClick = OpenClick
ReOpen.Caption = "&ReOpen"
ReOpen.Enabled = False
ReOpen1.Caption = ""           : ReOpen1.OnClick = Reopen1Click
ReOpen2.Caption = ""           : ReOpen2.OnClick = Reopen2Click
ReOpen3.Caption = ""           : ReOpen3.OnClick = Reopen3Click
Save.Caption = "&Save"         : Save.OnClick = SaveClick
SaveAs.Caption = "Save &As..." : SaveAs.OnClick = SaveAsClick
Break1.Caption ="-"
ExitEditor.Caption = "E&xit"   : ExitEditor.OnClick = ExitEditorClick

File.Caption = "&File"
File.AddItems New,Open,ReOpen,Save,SaveAs,Break1,ExitEditor

Copy.Caption = "&Copy"   : Copy.OnClick = CopyClick
Copy.ShortCut = "CTRL+C"
Cut.Caption  = "Cu&t"   : Cut.OnClick = CutClick
Cut.ShortCut = "CTRL+X"
Paste.Caption = "&Paste" : Paste.OnClick = PasteClick
Paste.ShortCut = "CTRL+V"
SelectAll.Caption = "&Select All" : SelectAll.OnClick = SelectAllClick

Edit.Caption = "&Edit"
Edit.AddItems Copy,Cut,Paste,SelectAll

Find.Caption = "&Find"
Find.OnClick = FindClick

Search.Caption = "&Search"
Search.AddItems Find

DecodUTF8.Caption = "DecodUTF8"
'DecodUTF8.OnClick =DecodUTF8sub ' ConvertLongCP
MainMenu.Parent = MainForm
MainMenu.AddItems File,Edit,Search,DecodUTF8

SrcEdit.Parent = MainForm
SrcEdit.Align = alClient
'SrcEdit.Width = MainForm.ClientWidth
'SrcEdit.Height = MainForm.ClientHeight
SrcEdit.ScrollBars = ssBoth
SrcEdit.PlainText = 0 'True
SrcEdit.WordWrap = False
SrcEdit.OnKeyUp =RichShowXY ' hiliteTxt
'SrcEdit.OnKeyPress = hiliteTxt
'SrcEdit.OnMouseDown = hiliteTxt
SrcEdit.OnMouseUp = hiliteTxt



SrcEdit.OnMouseMove = RichShowXY
SrcEdit.font=HiLiteFont

MainForm.Caption = "Simple, notepad like editor with syntax hilighting"
MainForm.Width = 800
MainForm.Height = 600
MainForm.Center
MainForm.OnResize = FormResize
MainForm.OnClose = FormClose

'--определение новой процедуры SrcEdit'
OldSrcEditWndProc = SetWindowLongAPI(SrcEdit.Handle, GWL_WNDPROC, CODEPTR(SrcEditWndProc)) 
'print "OldSrcEditWndProc=" ,OldSrcEditWndProc

DIM keyList AS QRichEdit
keyList.Parent = MainForm
keyList.top=SrcEdit.top+SrcEdit.Height+4
'keyList.left=20
keyList.Width =600
keyList.color=&HFAFFC2
keyList.visible=0

result= SendMessageAPI (SrcEdit.handle,EM_EXLIMITTEXT,0,65535*32)

IF fileExists(fullPath$+"keyword.lst") = 0 THEN
	''     PRINT "ERROR can't find file keyword.lst "+fullPath$+"keyword.lst"
	END
ELSE
	''     PRINT "Load file keyword.lst "+fullPath$+"keyword.lst"
	keyList.LoadFromFile(fullPath$+"keyword.lst")
END IF

'!!!---------------------------------------------

'FileName = "C:\_F\bas\rapidq\RQIDE\tstremmm26.bas" 'OpenDialog.FileName
FileName = "D:\_F\bas\rapidq\RQIDE\projects\RichEd\HLEditor2022-6remTab9good.bas"

'call  AddClrString ("427:FileName ="+(FileName ), clred, LogEdit)

if fileexists(FileName)>0 then
	SrcEdit.LoadFromFile FileName
else
	Showmessage ("File not found "+FileName)
end if

MainForm.ShowModal

'--- New  WinProc SrcEdit ---------------------------------------
FUNCTION SrcEditWndProc (hWnd AS LONG, uMsg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG
Result = CallWindowProc(OldSrcEditWndProc, hWnd, uMsg, wParam, lParam)

SELECT CASE uMsg
CASE  WM_PAINT '---- <--------------------------------------
	'print "SrcEdit WM_PAINT=",WM_PAINT
	'HideCaret hWnd
	
	hdc=GetDC(hWnd) : 'print "hdc=" ,hdc
	
	'__ Set the bitmap's backmode to transparent
	'previous_background_mode=SetBkMode (hDC, TRANSPARENT) 
	
	previous_background_mode=SetBkMode (hDC, OPAQUE) 
	
	'__ select font
	Curent = SelectObject(hdc, HiLiteFont.Handle)
	rgbPrev = SetTextColor(hdc, HiLiteFont.color)
	
	'__ Get visible rect
	
	M.WriteUDT(VisRect)
	result= SendMessageAPI (hWnd,EM_GETRECT,0,M.Pointer)
	M.Position = 0
	M.ReadUDT(VisRect)
	M.Position = 0
	'print "Rect.Left=",VisRect.Left, " Rect.top=",VisRect.top
	'print "Rect.Right=",VisRect.Right, " Rect.Bottom=",VisRect.Bottom
	
	'__ Get point structure (may be not needed)
	VisPoint.Left=VisRect.Left
	VisPoint.Top=VisRect.Top
	M.Position = 0
	M.WriteUDT(VisPoint)
	M.Position = 0
	'__Get char index from begin 
	lchar=SendMessageAPI (hWnd,EM_CHARFROMPOS,0,M.Pointer)
	'__ get first visible line 
	line_number= SendMessageAPI (hWnd,EM_LINEFROMCHAR,lchar,0 )
	'__ Hehe, more easy (get first visible line )
	FIRSTVISIBLELINE= SendMessageAPI (hWnd,EM_GETFIRSTVISIBLELINE,0,0)
	FirstChar=SendMessageAPI (hWnd,EM_LINEINDEX,line_number,0) 
	
	VisPoint.Left=VisRect.Right
	VisPoint.Top=VisRect.Bottom
	M.Position = 0
	M.WriteUDT(VisPoint)
	M.Position = 0
	Lastchar=SendMessageAPI (hWnd,EM_CHARFROMPOS,0,M.Pointer)'addr_rect 
	'print "Lastchar=" ,Lastchar
	
	'__ get LastVISIBLELINE 
	LastVISIBLELINE=SendMessageAPI (hWnd,EM_LINEFROMCHAR,Lastchar,0 )
	'print "LastVISIBLELINE=" ,LastVISIBLELINE
	
	hiliteTxt '-<<<<<<<<<<<<<<<<<<<<<<<<'
END SELECT

END FUNCTION


'-- *****************************************************************************************************************
SUB hiliteTxt 
DEFINT useEscapes = FALSE

DIM i       AS LONG
DIM j       AS LONG
DIM quote   AS BYTE
DIM ch      AS STRING * 1
DIM token   AS STRING
'---------- 
dim CurPos  AS LONG  '-- char pos  from file beginning  
dim StrPos  AS LONG  '-- firs char in current line  position   (from file beginning)   

'if SrcEdit.SelLength>0 then exit sub 'problem with selection hilighting

StrPos=FirstChar
CurPos=0
j=0
i=0
flDigit=-1

RhWnd=SrcEdit.Handle
'--  use  SrcEdit.line()    
FOR i =FIRSTVISIBLELINE  TO  LastVISIBLELINE  ' - throught visible lines
	quote = FALSE
	token = "" '  
	ch =""
	
	lenline1=len(SrcEdit.line(i))
	
	FOR j = 1 TO len(SrcEdit.line(i)) '- going through the characters in the current line
		CurPos=j +StrPos   '-  
		ch = SrcEdit.line(i)[j]     
		
		
		IF instr("+-=<>(){}\/^&*[]:;?,.%^#@!~_' "+chr$(34)+chr$(9), ch) THEN  '- if delimiter !!!
			
			' hilight previous token !!!!
			IF token <> "" THEN '- if token before - hilite it! 
				
				if instr(keyList.Text,ucase$(token))>0 then ' if digit ??? !!!
					'-------------------------
					SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-len(token)-1)
					rgbPrev = SetTextColor(hdc, clb)
					TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
					'-------------------------
				ELSEif flDigit=1 then
					'-------------------------
					SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-len(token)-1)
					rgbPrev = SetTextColor(hdc, clr)
					TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
					'-------------------------
					flDigit=-1
					
				ELSE
					flDigit=-1
				END IF
				token = "" ' clear token 
				flDigit=-1
			END IF
			
			
			
			IF ch = chr$(34) THEN        '--- begin string type -----!
				token = ""
				kk=0
				'-------------------------first qt set green color
				SendMessageAPI (rhWnd,EM_POSFROMCHAR, VisPoint1,CurPos-1)
				rgbPrev = SetTextColor(hdc, clGreen)
				TextOut(hdc, VisPoint1.left, VisPoint1.top, ch, len(ch))
				'-------------------------
				lenline=len(SrcEdit.line(i))
				FOR j = j+1 TO lenline  '---   char by char !
					CurPos=j +StrPos
					ch = SrcEdit.line(i)[j] 
					IF ch = chr$(34) THEN  '--- ------- end string! -----------
						
						'------------------------- last qt set green -------
						SendMessageAPI (rhWnd,EM_POSFROMCHAR, VisPoint1,CurPos-1)
						rgbPrev = SetTextColor(hdc, clGreen)                         
						TextOut(hdc, VisPoint1.left, VisPoint1.top, ch, len(ch))
						'-------------------------
						kk=1
						EXIT FOR
					else
						token = token + ch ' string token sum
					END IF
				NEXT J
				'j = j+1
				'---  end string type
				'------------------------- set string token to orange color --------------!! 
				SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-len(token)-kk)
				rgbPrev = SetTextColor(hdc, clOrange)
				TabbedTextOut (hdc, VisPoint1.left, VisPoint1.top, token,len(token),0,0,0)
				
				'TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
				kk=0
				token = "" ' clear token! 
				ch=""
				'-------------------------
				
			ELSEIF ch = "'" THEN '!-- comment begin ---------------
				RemF:
				'---first  comment pos
				RemPos=CurPos 
				
				token = ""   '- new token - comment, clear token! 
				
				rempos1=j 'RemPos ' rempos1=instr(SrcEdit.line(i),"'")
				
				token =mid$(SrcEdit.line(i),RemPos1, len(SrcEdit.line(i))-RemPos1+1) '! to enf of line
				'call  AddClrString ("573:token="+(token), clred, LogEdit)
				'call  AddClrString ("580:len(token)="+str$(len(token)), clred, LogEdit)
				j=j+len(token)
				CurPos=j +StrPos
				'---- comment ---------------
				SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,RemPos-1)
				rgbPrev = SetTextColor(hdc, clDGreen)
				TabbedTextOut (hdc, VisPoint1.left, VisPoint1.top, token,len(token),0,0,0)
				'TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
				'-------------------------
				ch = ""
				exit for
				
			ELSEIF ch = "/" THEN '
				ch1$ = SrcEdit.line(i)[j+1]
				'call  AddClrString ("536:ch1$="+(ch1$), clred, LogEdit)
				if ch1$ = "/" then goto RemF:
				
			ELSEIF ch = chr$(9) THEN '---- tab  !!!!!!!!!!!! replaced by 4 spaces in source
				previous_background_mode=SetBkMode (byval hDC, byval OPAQUE) 
				SendMessageAPI (rhWnd,EM_POSFROMCHAR, VisPoint1,CurPos-1)
				rgbPrev = SetTextColor(hdc, clo)
				bgcolor= GetBkColor(hDC)
				
				SetBkColor(hdc,clGreen)
				'call  AddClrString ("579:rgbPrev="+str$(rgbPrev), clred, LogEdit)
				TextOut(hdc, VisPoint1.left+2, VisPoint1.top, ch, len(ch))
				SetBkColor(hdc,bgcolor)
				ch = ""
				
			elseIF instr("+-=<>\/^&*[]:;?,.", ch) THEN
				'-------------------------
				SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-1)
				rgbPrev = SetTextColor(hdc, &HCA9000)
				TextOut(hdc, VisPoint1.left, VisPoint1.top, ch, len(ch))
				'-------------------------
			ELSEIF instr("()[]{}", ch) THEN
				'-------------------------
				SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-1)
				rgbPrev = SetTextColor(hdc, cla)
				TextOut(hdc, VisPoint1.left, VisPoint1.top, ch, len(ch))
				'-------------------------
				
			ELSEIF token <> "" THEN
			END IF
			
			token = ""
			
		elseIF instr("1234567890", ch) THEN 
			
			if flDigit=-1 then flDigit=1
			
			IF flDigit=1 THEN 
			ELSE
				flDigit=0
				
			END IF
			token = token + ch
			
		else
			token = token + ch
			flDigit=0
		END IF
		
	NEXT j
	
	IF token <> "" THEN
		if instr(keyList.Text,ucase$(token))>0 then
			'-------------------------
			SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-len(token))
			rgbPrev = SetTextColor(hdc, clb)
			TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
			'-------------------------
		ELSEif flDigit=1 then
			'-------------------------
			SendMessageAPI (rhWnd,EM_POSFROMCHAR,VisPoint1,CurPos-len(token))
			rgbPrev = SetTextColor(hdc, clr)
			TextOut(hdc, VisPoint1.left, VisPoint1.top, token, len(token))
			'-------------------------
			flDigit=-1
		END IF
	END IF
	StrPos=StrPos+len(SrcEdit.line(i))+2'StrPos+j 
	'call  AddClrString ("657:StrPos="+str$(StrPos), &H006435, LogEdit)
	
NEXT i
END SUB ' hiliteTxt


'!************************************************************************'
sub R2Change

RichRow1=str$(SrcEdit.WhereY+1)
RichCol1=str$(SrcEdit.WhereX+1)
'call  AddClrString ("8293:RichCol1="+str$(RichCol1), clred, LogEdit)

if SrcEdit.SelStart+1< len(SrcEdit.Text) then 
	pos1=SrcEdit.SelStart+1
else
	pos1=SrcEdit.SelStart
end if

char1$ = str$(asc(mid$(SrcEdit.Text, pos1,1)))
'RichX1.Caption= RichRow1+":"+RichCol1 +"    "+char1$
'RichX1.Font.Color=clBlue
SrcEditLen1=len (SrcEdit.text)

'SrcEdit.SelStart=CursWin(WindowsIndex) 

''    AddPanels "line ", "col ", "ASCII","char pos"
StatusBar.Panel(0).caption = "Row "+RichRow1+":"+  str$(SrcEdit.LineCount)
StatusBar.Panel(1).caption = "Col "+RichCol1  
StatusBar.Panel(3).caption = "Asc "+char1$
StatusBar.Panel(2).caption = "Pos "+str$(pos1)+":"+  str$(SrcEditLen1)

if SrcEditLen<>SrcEdit.LineCount then 'SrcEditLen1 только при изменении числа строк
	SrcEditLen=SrcEdit.LineCount
	Modiflg=1
end if
'call  AddClrString ("8317:Modiflg="+str$(Modiflg), clred, LogEdit)

END SUB
'AddPanels "Row ", "Col ", "Pos ","Asc ", "EOL ",""  

diakin
Posts: 102
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Re: Syntax hilight example

Post by diakin »

FreeBasic dll example

Code: Select all

'HiLightDllv4.bas

'option explicit
#ifdef FB__WIN32
'$include: 'inc\win\kernel32.bi'
#endif
'$INCLUDE: 'crt.bi'


Type QPoint
Left As Long
Top As Long
End Type

type QRECT 
left As Long
top  As Long
right  As Long
bottom  As Long
End Type


TYPE  CHARRANGE 
cpMin   AS  long              
cpMax   AS  long              
END TYPE

flsel=-1



#define EM_SETBKGNDCOLOR  1091
#define WM_SETTEXT 12
'#define EM_POSFROMCHAR
'-- messages
#define WM_DESTORY  2
#define WM_PAINT  &HF

#define clBlack  0
'Private Const 
#define COLOR_BTNFACE  15

'-- BGR
#define clBlue  &HFF0000  '256
#define clLBlue  &HFFaa00  '256clLtBlue
#define clLtBlue  &HFFaa00  '256clLtBlue
#define clDBlue  &H800000 '128

#define clMaroon  &H808000

#define clGreen  &H00FF00
#define clDGreen  &H008000

#define clLRed  &H8080FF
#define clRed    &H0000FF
#define clDRed  &H000080

#define clPurple  &HFF00FF
#define clDPurple &h800080

#define clLMagenta  &HFF00FF
#define clMagenta &h800080

#define clSilver  &hc0c0c0
#define clGrey &h808080
#define clGray &h808080
#define clDGray &h808080
#define clDGrey &h808080
#define clWhite  &HFFFFFF

#define clAqua &hffff00
#define clLAqua &hfffff0

#define clyellow &h00ffff
#define clBrown &h008080
#define clOrange &h0080FF

#define  WM_COPY  769
#define  EM_EXGETSEL  1076


'-- Edit Control Messages
#define EM_EXLIMITTEXT  1077
#define EM_GETSEL  &HB0
#define EM_SETSEL  &HB1
#define EM_GETRECT  &HB2
#define EM_SETRECT  &HB3
#define EM_SETRECTNP  &HB4
#define EM_SCROLL  &HB5
#define EM_LINESCROLL  &HB6
#define EM_SCROLLCARET  &HB7
#define EM_GETMODIFY  &HB8
#define EM_SETMODIFY  &HB9
#define EM_GETLINECOUNT  &HBA
#define EM_LINEINDEX  &HBB
#define EM_SETHANDLE  &HBC
#define EM_GETHANDLE  &HBD
#define EM_GETTHUMB  &HBE
#define EM_LINELENGTH  &HC1
#define EM_REPLACESEL  &HC2
#define EM_GETLINE  &HC4
#define EM_LIMITTEXT  &HC5
#define EM_CANUNDO  &HC6
#define EM_UNDO  &HC7
#define EM_FMTLINES  &HC8
#define EM_LINEFROMCHAR  &HC9
#define EM_SETTABSTOPS  &HCB
#define EM_SETPASSWORDCHAR  &HCC
#define EM_EMPTYUNDOBUFFER  &HCD
#define EM_GETFIRSTVISIBLELINE  &HCE
#define EM_SETREADONLY  &HCF
#define EM_SETWORDBREAKPROC  &HD0
#define EM_GETWORDBREAKPROC  &HD1
#define EM_GETPASSWORDCHAR  &HD2
#define EM_CHARFROMPOS  &HD7
#define EM_POSFROMCHAR  &HD6
#define EM_GETSELTEXT  1086


' Background Modes
#define TRANSPARENT  1
#define OPAQUE  2
#define BKMODE_LAST  2

'-- EDITWORDBREAKPROC code values
#define WB_LEFT  0
#define WB_RIGHT  1
#define WB_ISDELIMITER  2


#define GWL_WNDPROC  (-4)
#define GWL_HWNDPARENT  (-8)

Declare Function TabbedTextOut Lib "user32" Alias "TabbedTextOutA" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, _
ByVal nCount As Long, ByVal nTabPositions As Long, lpnTabStopPositions As Long, ByVal nTabOrigin As Long) As Long


Declare Function SendMessageA LIB "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer,  byref lParam As any ) As Integer

Declare Function GetDC Lib "user32" Alias "GetDC"(ByVal hWnd As Long) As Long

Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
(ByVal hwnd As Long, ByVal hdc As Long) As Long

Declare Function SetBkMode Lib "gdi32" Alias "SetBkMode" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, byref lpString As any, ByVal nCount As Long) As Long

Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function SetTextColor Lib "gdi32" Alias "SetTextColor" (ByVal hdc As Long, ByVal crColor As Long) As Long

Declare Function FillRect Lib "user32" Alias "FillRect" _ 
(ByVal hdc As Long, lpRect As QRect, ByVal hBrush As Long) As Long

Declare Function GetBkColor Lib "gdi32" Alias "GetBkColor" (ByVal hdc As Long) As Long
Declare Function SetBkColor Lib "gdi32" Alias "SetBkColor" (ByVal hdc As Long, ByVal crColor As Long) As Long

Declare Function DrawText Lib "user32" Alias "DrawTextA" _
(ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
lpRect As QRECT, ByVal wFormat As Long) As Long

Declare Function CreateSolidBrush Lib "gdi32" Alias "CreateSolidBrush" (ByVal crColor As Long) As Long

'declare function HiLight  alias "HiLight" (byval RhWnd as long, byval HiLiteFontHandle as long) as long
'declare function HiLight  alias "HiLight" (byval RhWnd as long, byval HiLiteFontHandle as long, byval  ptrKeyList As byte ptr, byval hthl as long) as long
'declare function HiLight  alias "HiLight" (byval RhWnd as long, byval HiLiteFontHandle as long,byval HiLiteFontBoldHandle as long, byval  ptrKeyList As byte ptr, byval hthl as long) as long

declare function HiLight  alias "HiLight" (byval RhWnd as long, byval HiLiteFontHandle as long, ptrHLClrs as long ptr, byval  ptrKeyList As byte ptr, byval hthl as long) as long

' ---$include: 'DllMain.bi'
'-- *****************************************************************************************************
Function HiLight (byval RhWnd as long, byval HiLiteFontHandle as long, ptrHLClrs as long ptr,  byval ptrKeyList As byte ptr, byval hthl as long) as long export

DIM xp As long Ptr:  xp = @ptrHLClrs : DIM HiLiteColors(0 to 15) AS long : MEMCPY(@HiLiteColors(0), xp, 16 * SIZEOF(long))
'DIM xp As Single Ptr:  xp = @lpxr : DIM xr(n) AS SINGLE : MEMCPY(@xr(0), xp, n * SIZEOF(SINGLE))

'print "ptrKeyList="; ptrKeyList
'dim hBrush as long
'hBrush = CreateSolidBrush(clLBlue)

dim VisPoint as QPoint
dim VisPoint1 as QPoint
dim VisRect as QRect

dim VisRectPtr as QRect ptr
VisRectPtr=@VisRect

dim CHARRANGE1 as CHARRANGE
dim CHARRANGEPtr as CHARRANGE ptr
CHARRANGEPtr=@CHARRANGE1



dim hdc as integer '-- device handle'
dim previous_background_mode as long,rgbPrev as long
dim Curent as integer 
dim result1 as integer ,kk as long,RemFlg as long,RemPos as long,RemPos1 as long
dim bgcolor as long

dim keyListText as string
'keyListText=LoadString ("keyword.lst")
keyListText=string$(3048,"-")
'print "1 keyListText=" ;len(keyListText)
keyListText= *ptrKeyList

'print "2 keyListText=" ;len(keyListText)

'"DIM CREATE"
dim _crlf as byte
_crlf=2

if hthl =0 then 
	hthl =0
	_crlf=1
elseif hthl =1 then
	hthl =1
	_crlf=1
	
elseif hthl =2 then
	hthl =0
	_crlf=2
	
elseif hthl =3 then
	hthl =1
	_crlf=2
	
else
end if

'print"hthl=";hthl
'print"crlf=";_crlf

DIM i       AS LONG
DIM j       AS LONG , j1 as long
DIM quote   AS BYTE
DIM ch      AS STRING'' * 1
DIM token   AS STRING
'----------
dim CurPos  AS LONG  '-- char pos  from file beginning  
dim StrPos  AS LONG  '-- firs char in current line  position   (from file beginning)   

dim lchar   AS LONG  
dim line_number   AS LONG  ,LINELENGTH  AS LONG 
dim  FirstChar  AS LONG  
dim  LastChar  AS LONG  
dim  FIRSTVISIBLELINE  AS LONG  
dim  LastVISIBLELINE  AS LONG  
dim  richeditline as string
dim rline as byte ptr

richeditline=string$(2,"-")
rline=sadd (richeditline)

hdc=GetDC(RhWnd) : 'print "hdc=" ,hdc

'__ Set the bitmap's backmode to transparent
'previous_background_mode=SetBkMode (hDC, TRANSPARENT) 
previous_background_mode=SetBkMode (hDC, OPAQUE) 


'__ select font
Curent = SelectObject(hdc, HiLiteFontHandle)
SetTextColor(hdc, HiLiteColors(9)) '&H00ffFF текст цветом фона
SetBkColor(hdc,HiLiteColors(0))

'__ Get visible rect

result1= SendMessageA (rhWnd,EM_GETRECT,0,  VisRect)

'__ Get point structure (may be not needed)
VisPoint.Left=VisRect.Left
VisPoint.Top=VisRect.Top

'__Get char index from begin 
lchar=SendMessageA (rhWnd,EM_CHARFROMPOS,0,VisPoint)

'__ get first visible line 
line_number= SendMessageA (rhWnd,EM_LINEFROMCHAR,byval lchar,0 )

'__ Hehe, more easy (get first visible line )
FIRSTVISIBLELINE= SendMessageA (rhWnd,EM_GETFIRSTVISIBLELINE,0,0)
FirstChar=SendMessageA (rhWnd,EM_LINEINDEX,byval line_number,0) 

VisPoint.Left=VisRect.Right
VisPoint.Top=VisRect.Bottom
Lastchar=SendMessageA (rhWnd,EM_CHARFROMPOS,0, VisPoint)'addr_rect 

'__ get LastVISIBLELINE 
LastVISIBLELINE=SendMessageA (rhWnd,EM_LINEFROMCHAR,byval Lastchar,0 )


SendMessageA (rhWnd,EM_EXGETSEL, byval 0, byval @CHARRANGE1)
'SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint,byval CHARRANGE1.cpMin)

lensel=CHARRANGE1.cpMax-CHARRANGE1.cpMin
'print"296:lensel=";lensel

LineCount=SendMessageA (rhWnd,EM_GETLINECOUNT, 0, 0)
'print"299:LineCount=";LineCount


StrPos=FirstChar
CurPos=0
j=0
i=0
flDigit=-1

'print"343:CHARRANGE1.cpMin=";CHARRANGE1.cpMin
'print"343:CHARRANGE1.cpMax=";CHARRANGE1.cpMax

FOR i =1  TO LineCount-1  '-   перебираем по строкам
	
	FirstChar=SendMessageA (byval rhWnd,EM_LINEINDEX, i,0) '!! первый символ i-той строки  
	LINELENGTH =SendMessageA (byval rhWnd,EM_LINELENGTH ,byval FirstChar,0) 
	
	if LINELENGTH >0 then 
		richeditline=string$(LINELENGTH,"-") ' create buffer
		rline=sadd (richeditline)
	else
		richeditline=""
		rline=sadd (richeditline)
		goto nexti1
	end if
	
	SendMessageA (rhWnd,EM_GETLINE, byval i, byval rline)
	novFlag=0
	
	if instr(richeditline,"/*")>0  then flsel=1
	
	if instr(richeditline,"*\")>0  and flsel=1 then 
		flsel=0
	end if
	
	if i> FIRSTVISIBLELINE then exit for
	nexti1:
next i




FOR i =FIRSTVISIBLELINE  TO LastVISIBLELINE  '-   count visible lines !
	quote = FALSE
	token = ""
	ch =""
	
	FirstChar=SendMessageA (byval rhWnd,EM_LINEINDEX, i,0) '!! первый символ i-той строки  
	LINELENGTH =SendMessageA (byval rhWnd,EM_LINELENGTH ,byval FirstChar,0) 
	
	if LINELENGTH >0 then 
		richeditline=string$(LINELENGTH,"-") ' create buffer
		rline=sadd (richeditline)
	else
		richeditline=""
		rline=sadd (richeditline)
		goto nexti
	end if
	SendMessageA (rhWnd,EM_GETLINE, byval i, byval rline)
	novFlag=0
	
	'! пропускаем подсветку выделения 
	'lensel=CHARRANGE1.cpMax-CHARRANGE1.cpMin
	'print"289:CHARRANGE1.cpMin=";CHARRANGE1.cpMin
	'print"289:CHARRANGE1.cpMax=";CHARRANGE1.cpMax
	
	'print"========= 330:FirstChar=";FirstChar
	
	' if FirstChar>= CHARRANGE1.cpMin  and (FirstChar +len(richeditline)) <= CHARRANGE1.cpMax then
	'goto nexti
	'end if
	
	
	
	' print"richeditline=";richeditline
	
	FOR j = 1 TO len(richeditline) '- '-  chars in the current line i ! 
		Curent = SelectObject(hdc, HiLiteFontHandle)
		CurPos=j +StrPos   '-  
		ch = mid$(richeditline,j,1) ' 
		
		' if char position is in the selection range - not hilight it !
		if CurPos>= CHARRANGE1.cpMin and CurPos <= CHARRANGE1.cpMax then
			
			flsel=1 ' !!!!
			'print"351:flsel=";flsel
		end if
		
		IF instr("+-=<>(){}\/^&*[]:;?,.#!@$%~`_' "+chr$(34)+chr$(9), ch) >0 THEN  ' если разделитель токена ! 
			
			'  !!!!
			IF token <> "" THEN '- if token before - hilite it! 
				if flsel<>1 then
					
					if instr(keyListText,ucase$(token))>0 and flsel=0 then ' if token in keylist !
						'-------------------------
						SendMessageA (rhWnd,EM_POSFROMCHAR,byval @VisPoint1,byval CurPos-len(token)-1)
						SetTextColor(hdc,  HiLiteColors(1)) ' clPurple) '''purple   HiLiteColors(2) 
						TextOut(hdc,  byval VisPoint1.left,  byval VisPoint1.top,  byval token,  byval len(token))
						'-------------------------
					ELSEif flDigit=1   and flsel=0  then' !
						'-------------------------
						SendMessageA (rhWnd,EM_POSFROMCHAR,byval @VisPoint1,byval CurPos-len(token)-1)
						SetTextColor(hdc, HiLiteColors(8) ) ' clPurple) '''  !!!
						TextOut(hdc,  byval VisPoint1.left,  byval VisPoint1.top,  byval token,  byval len(token))
						'-------------------------
						flDigit=-1
						
					ELSE
						flDigit=-1
					END IF
				END IF
				
				token = "" ' очищаем токен  !!
				flDigit=-1
			END IF
			
			
			IF ch = chr$(34) THEN        '--- ------- if quote ----------- !
				token = ""
				kk=0
				
				if flsel=0 or lensel=0 then
					
					'------------------------- hilite first quot ---!
					SendMessageA (rhWnd,EM_POSFROMCHAR,byval  @VisPoint1,byval CurPos-1)
					SetTextColor(hdc, HiLiteColors(5) ) ' clPurple) '''фиолет   
					TextOut(hdc,  byval VisPoint1.left,  byval VisPoint1.top,  byval ch,  byval len(ch))
				end if
				
				'-------------------------
				FOR j1 = j+1 TO len(richeditline) '---  from begin string type to eol
					CurPos=j1 +StrPos
					
					' sel range - not hilight it!
					if CurPos>= CHARRANGE1.cpMin and CurPos <= CHARRANGE1.cpMax then
						
						flsel=1 ' !!!
					end if
					
					
					ch = mid$(richeditline,j1,1) 
					
					IF ch = chr$(34) THEN  '--- ------- if last quote -----------!
						if flsel=0 or lensel=0 then
							'-------------------------
							SendMessageA (rhWnd,EM_POSFROMCHAR, @VisPoint1, byval CurPos-1)
							SetTextColor(hdc, HiLiteColors(5) ) 'clPurple)                         
							TextOut(hdc,  byval VisPoint1.left,  byval VisPoint1.top, byval ch,  byval len(ch))
						END IF
						'-------------------------
						kk=1
						EXIT FOR
					else
						token = token +ch'' store quote token!
					END IF
				NEXT j1
				j=j1 ' correct cursor position !!!
				'---  end string type
				
				if flsel=0 or lensel=0 then
					
					'------------------------- hilite quote token !!
					SendMessageA (byval rhWnd,EM_POSFROMCHAR,byval @VisPoint,byval CurPos-len(token)-kk)
					SetTextColor(hdc, HiLiteColors(7) ) 'clOrange)
					'TextOut(hdc, VisPoint.left, VisPoint.top, byval token,  byval len(token))
					TabbedTextOut (hdc, VisPoint.left, VisPoint.top, token,-1,0,0,0)
				END IF
				
				'-------------------------
				token = "" ' 
				ch = ""
				'flsel=0 ' !
				
				
			ELSEIF ch = "'" THEN '-- comment ---------------!
				
				RemF:
				
				RemPos=CurPos  '---first pos of comment
				'token = ""   '- new token - comment
				
				rempos1= j '=instr(richeditline,"'")  
				
				token =mid$(richeditline,RemPos1)'  from rempos1 to end of line !, len(richeditline)-RemPos1+1)  
				j=j+len(token)
				CurPos=j +StrPos ' это же конец строки
				
				' !
				'if RemPos <= CHARRANGE1.cpMin and CurPos <= CHARRANGE1.cpMax then
				if RemPos <= CHARRANGE1.cpMin and lensel>0 then
					
					flsel=1 '
				end if
				RemFML:
				' multiline comment  ???????????????????
				'2=instr(CurPos,token,"!!!!!")
				
				
				if flsel=0 or lensel=0 then ' hilight it !!
					
					'-------------------------
					SendMessageA (byval rhWnd,EM_POSFROMCHAR, byval @VisPoint1, byval RemPos-1)
					if instr(token,"!!!!!")>0 then
						SetTextColor( byval hdc, &H83FFFF ) 'clred)
					elseif instr(token,"!!!!")>0 then
						SetTextColor( byval hdc, &HAE59FF ) 'clred)
					elseif instr(token,"!!!")>0 then
						SetTextColor( byval hdc, HiLiteColors(8) ) 'clred)
					elseif instr(token,"!!")>0 then
						SetTextColor( byval hdc, &H00B3FF ) 'clo
					elseif instr(token,"!")>0 then
						SetTextColor( byval hdc, &HD7E400 ) 'cla
					else
						SetTextColor( byval hdc, HiLiteColors(6) ) 'clDGreen)
					end if
					
					TextOut( byval hdc,  byval VisPoint1.left,  byval VisPoint1.top, byval token,  byval len(token))
				end if
				
				flsel=0
				ch = ""
				exit for
				
				
			ELSEIF ch = "/" THEN ' C++ comment !
				ch1$ = mid$(richeditline,j+1,1)       
				if ch1$ = "/" then goto RemF:
				if ch1$ = "*" then goto RemFML:
				
			ELSEIF ch = chr$(9) THEN '---- tab  !!! !!!!!!!!!!!! replaced by 4 spaces in source
				if hthl =0 then goto noht
				bgcolor= GetBkColor(hDC)
				rgbPrev = SetTextColor(hdc, bgcolor)
				previous_background_mode=SetBkMode (byval hDC, byval OPAQUE) 
				SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint1, byval CurPos-1)
				SetBkColor(hdc,HiLiteColors(15))
				TextOut(hdc, byval VisPoint1.left+2, byval VisPoint1.top, byval ch, byval len(ch))
				SetBkColor(hdc,HiLiteColors(0))
				noht:
				ch = ""
				
			elseIF instr("+-=<>\/^&*[]:;?,.$@", ch) THEN ' brackets\quotes !
				
				if flsel=0 or lensel=0 then ' hilight it !!
					
					'-------------------------
					SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint, byval CurPos-1)
					SetTextColor(hdc, HiLiteColors(2) ) 'clBlue)
					TextOut(hdc, VisPoint.left, VisPoint.top, byval ch, len(ch))
				end if
				'-------------------------
			ELSEIF instr("()[]{}", ch) THEN
				if flsel=0 or lensel=0 then ' hilight it !!
					'-------------------------
					SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint, byval CurPos-1)
					SetTextColor(hdc, HiLiteColors(3) ) 'clBlue)
					TextOut(hdc, VisPoint.left, VisPoint.top, byval ch, len(ch))
				end if
				
				'ELSEIF len(token) > 1 THEN '!! hilight end of token
			ELSEIF token <> "" THEN
				
			END IF
			
			token = "" '!!!!<<<<<<<<<<<<<<<<<<<<<<<<
			'flsel=0
		ELSEif instr("0123456789", ch)>0  THEN '!  value and novFlag=0
			
			if flDigit=-1 then flDigit=1
			
			IF flDigit=1 THEN 
			ELSE
				flDigit=0
				
			END IF
			token = token + ch
			
		ELSE
			token = token + ch
			flDigit=0
		END IF
		nextj:
	NEXT j
	
	
	IF len(token) > 0 and (flsel=0  or lensel=0) THEN  ' end of the line !!!!!
		
		if instr(keyListText,ucase$(token))>0  then
			
			'-------------------------
			SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint,byval CurPos-len(token))
			SetTextColor(hdc, HiLiteColors(1) ) 'clblue)
			TextOut(hdc, VisPoint.left, VisPoint.top, byval token, byval len(token))
			'token = ""
			
			'-------------------------
			'*'                fOut.write("<font color=\"000099\"><b>"+token+"</b></font>")
			
			ELSEIF ucase$(token)="SHL" or ucase$(token)="SHR" or ucase$(token)="MOD" or ucase$(token)="INV" _
		or ucase$(token)="NOT" or ucase$(token)="AND" or ucase$(token)="OR" or ucase$(token)="XOR"  THEN 
			'!!------------------------- operators ------
			SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint,byval CurPos-len(token))
			SetTextColor(hdc, HiLiteColors(3) ) 'clDPurple)
			TextOut(hdc, VisPoint.left, VisPoint.top, byval token, byval len(token))
			'token = ""
			
		ELSEif flDigit=1 then
			'-------------------------
			SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint,byval CurPos-len(token))
			SetTextColor(hdc, HiLiteColors(8) ) 'clDPurple)
			TextOut(hdc, VisPoint.left, VisPoint.top, byval token, byval len(token))
			'-------------------------
			flDigit=-1
			
		ELSE
			'ELSEIF instr("0123456789", token)>0 THEN 
			'*'                fOut.write(token)
		END IF
	else
		'print ch
		'if ch=chr$(13) then print "chr$(13)"
		'if ch=chr$(10) then print "chr$(10)"
	END IF
	nexti:
	StrPos=StrPos+len(richeditline)+_crlf 'StrPos+j 
NEXT i

SendMessageA (rhWnd,EM_EXGETSEL, byval 0, byval @CHARRANGE1)
'SendMessageA (rhWnd,EM_POSFROMCHAR, byval @VisPoint,byval CHARRANGE1.cpMin)

lensel=CHARRANGE1.cpMax-CHARRANGE1.cpMin
'print"589:CHARRANGE1.cpMin=";CHARRANGE1.cpMin
'print"589:CHARRANGE1.cpMax=";CHARRANGE1.cpMax


ReleaseDC(rhWnd, hDC)
END function


diakin
Posts: 102
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Re: Syntax hilight example

Post by diakin »

Problems
- code is ugly
- problems with highlighting selected text
- problems with highlighting multiline comments and quotes

Maybe someone have another example of the source highlighting ?
diakin
Posts: 102
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Re: Syntax hilight example

Post by diakin »

keyword.lst file

Code: Select all

FILEEXISTS
BYVAL
BYREF
EXECUTE
CEIL
FLOOR
MEMCMP
CALL
DIM
REDIM
AS
CREATE
IF
THEN
ELSE
ELSEIF
END
$MACRO
$OPTION
$INCLUDE
$RESOURCE
$TYPECHECK
$OPTIMIZE
$ESCAPECHARS
$IFDEF
$IFNDEF
$ENDIF
$APPTYPE
$ELSE
$DEFINE
$UNDEF
EVENT
INTEGER
STRING
SHORT
LONG
SINGLE
DOUBLE
WORD
DWORD
VARIANT
BYTE
TYPE
EXTENDS
PRINT
LPRINT
LFLUSH
SUB
MESSAGEBOX
UNLOADLIBRARY
LIBRARYINST
FUNCTION
SUBI
FUNCTIONI
ALIAS
LIB
DECLARE
BIND
CALLFUNC
FOR
TO
NEXT
STEP
DO
LOOP
UNTIL
WHILE
WEND
EXIT
GOTO
GOSUB
RETURN

diakin
Posts: 102
Joined: May 28, 2005 6:06
Location: Russia, St-Petersburg
Contact:

Re: Syntax hilight example

Post by diakin »

Screenshots in https://raduino.com
Post Reply