Simple VBA Macro for Syntax Highlight

Started by PapoAnaya, October 24, 2005, 12:22:25 PM

Previous topic - Next topic

PapoAnaya

Hi:



I wrote the following small macro for syntax highlight.  It's very basic, but demonstrates how newLISP can be used when you are not in the mood of coding a lot of VBA code for mundane things like that. (I guess I could've used VBA string search routines, but I wanted to try it out anyway. :)



Papo



' newlisp preable
Public Declare Function dllEvalNewLISP Lib "c:program filesnewlispnewlisp.dll" Alias "newlispEvalStr" (ByVal LExpr As String) As Long
Private Declare Function lstrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long
Private Declare Function lstrCpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, lpString2 As Any) As Long
Private Declare Function LoadLibraryA Lib "kernel32" (ByVal s As String) As Long
Private Declare Sub FreeLibrary Lib "kernel32" (ByVal h As Long)
Dim NewLISPhandle As Long

Function EvalNewLISP(LispExpression As String) As String
Dim resHandle As Long
Dim result As String
resHandle = dllEvalNewLISP(LispExpression)
result = Space$(lstrLen(ByVal resHandle))
lstrCpy ByVal result, ByVal resHandle
EvalNewLISP = result
End Function

'Gets a keyword from a list, returns "member" value
Function GetKeyword(inkey As String)
    Dim statement As String
    Dim keywords As String
    keywords = "(setq kw '(expr strsub executesqlsimple strcat puts set mktime unixtime while for incr dbapi loaddriver if else elsif ))"
    statement = "(member ' " + inkey + " kw)"
    output = EvalNewLISP(keywords)
    output = EvalNewLISP(statement)
    GetKeyword = output
End Function

' goes to a selection, if the keyword exists, then boldify, otherwise, leave
' alone

Sub syntax_highlight()
   Dim CurrentString As String
   For i = 1 To Selection.Words.Count
      CurrentString = Selection.Words(i).Text
      If GetKeyword(CurrentString) <> "nil" Then
         Selection.Words(i).Bold = True
      End If
   Next i
End Sub