make your own custom keyword highlighting

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

make your own custom keyword highlighting

#1 Post by big_bass »

after spending some time with just trying to understand C coded lexers :roll:
I decided that bash and linux commands are much easier to get a
working test run going quickly and I think some cool tools will
result if we have it scriptable

since there isnt yet an official Bacon/HUG tool for this it would be great to use as an
example

the why I did this was because posting code in the forum
wont allow any automatic keyword coloring

this isnt a big problem but it is annoying
so why not make a tool to read a list of keywords and add color
and make it compatible with the forum's phpbb code :idea:

thats exactly what I did look at the example below
that would be a big pain to manually add
but easy with a script
REM
REM H.U.G. - Highlevel Universal GUI implementation for BaCon based on GTK
REM (c) Peter van Eerten - August 2009 / July 2011, GPL.
REM
REM ------------------------------------------------------------------------------------------------
REM MacOSX notes: the previous installer for GTK in Quartz puts the libraries in
REM
REM - /Library/Frameworks/Gtk.framework/Libraries/
REM - /Library/Frameworks/Glib.framework/Libraries/
REM
REM HUG expects the above generic directories also. The new GTK builder from
REM http://gtk-osx.sourceforge.net/ puts the libraries in some local directory.
REM
REM Please create the above directories as symlinks to this local directory if using this builder.
REM
REM --------------------------------------------------------------------------------------------------
REM Version 0.5 - MAJOR RELEASE
REM - implemented optional OpenGL canvas using GtkGlArea
REM - ported PIXEL , LINE , CIRCLE and SQUARE to OpenGL
REM - no scaling for canvas, more preparations for selective INCLUDE functionality
REM - changed internal housekeeping to associative arrays which saves almost 900 lines of code
REM Version 0.51 - implemented GtkGlExt as a backend for canvas
REM Version 0.52 - renamed variables in function arguments to avoid clash with CONST
REM Version 0.53 - improved DRAW and SYNC for OpenGL for non-HUG drawing
REM Version 0.54 - ported the OUT directive to OpenGL
REM Version 0.55 - improved heuristic library search, code shrunk about 150 lines more
REM Version 0.56 - ability to explicitly choose which GL library to use, bugfix in HUGLIB$
REM Version 0.57 - better GL drawing, option GL FREEZE/UNFREEZE to improve GL drawing performance
REM Version 0.58 - resizable canvas when using the TABLE layer
REM Version 0.59 - REGISTER to register external widgets into HUG, fixed bug with GRAB $
REM Version 0.60 - METHOD to register the default GRAB $/TEXT /GET /SET functions to external widgets
REM Version 0.61 - fixed titles for FILEDIALOG
REM Version 0.62 - TEXT can delete last added entry to COMBO
REM Version 0.63 - More bugfixes with COMBOBOX
REM --------------------------------------------------------------------------------------------------

' When used as an include file, always call Init
CALL INIT

REM --------------------------------------------------------------------------------------------------

FUNCTION hug_Get_Gtk$ : REM INCLUDE

LOCAL hug_lib$
LOCAL hug_sequence = -1

IF INSTR (OS$, "Darwin") THEN
hug_lib$ = "/opt/local/lib/libgtk-x11-2.0.0.dylib"
IF NOT (FILEEXISTS (hug_lib$)) THEN
hug_lib$ = "/Library/Frameworks/Gtk.framework/Libraries/libgtk-quartz-2.0.0.dylib"
END IF
END IF

CATCH GOTO Redo_Import

LABEL Redo_Import
INCR hug_sequence
IF hug_sequence = HUG_MAX_LIB_SEQ THEN GOTO Print_Error
IF NOT (INSTR (hug_lib$, "dylib")) THEN hug_lib$ = CONCAT $("libgtk-x11-2.0.so.", STR $(hug_sequence))
IMPORT "gtk_init(int*,void*)" FROM hug_lib$ TYPE void

RETURN hug_lib$

LABEL Print_Error
PRINT "Gtk library not found!"
END

END FUNCTION

REM --------------------------------------------------------------------------------------------------

FUNCTION hug_Get_Gdk$ : REM INCLUDE

LOCAL hug_lib$
LOCAL hug_sequence = -1

IF INSTR (OS$, "Darwin") THEN
hug_lib$ = "/opt/local/lib/libgdk-x11-2.0.0.dylib"
IF NOT (FILEEXISTS (hug_lib$)) THEN
hug_lib$ = "/Library/Frameworks/Gtk.framework/Libraries/libgdk-quartz-2.0.0.dylib"
END IF
END IF

CATCH GOTO Redo_Import

LABEL Redo_Import
INCR hug_sequence
IF hug_sequence = HUG_MAX_LIB_SEQ THEN GOTO Print_Error
IF NOT (INSTR (hug_lib$, "dylib")) THEN hug_lib$ = CONCAT $("libgdk-x11-2.0.so.", STR $(hug_sequence))
IMPORT "gdk_atom_intern(char*,int)" FROM hug_lib$ TYPE long

RETURN hug_lib$

LABEL Print_Error
PRINT "Gdk library not found!"
END

END FUNCTION

REM --------------------------------------------------------------------------------------------------

FUNCTION hug_Get_Glib$ : REM INCLUDE

LOCAL hug_lib$
LOCAL hug_sequence = -1

IF INSTR (OS$, "Darwin") THEN
hug_lib$ = "/opt/local/lib/libglib-2.0.0.dylib"
IF NOT (FILEEXISTS (hug_lib$)) THEN
hug_lib$ = "/Library/Frameworks/Glib.framework/Libraries/libglib-2.0.0.dylib"
END IF
END IF

CATCH GOTO Redo_Import

LABEL Redo_Import
INCR hug_sequence
IF hug_sequence = HUG_MAX_LIB_SEQ THEN GOTO Print_Error
IF NOT (INSTR (hug_lib$, "dylib")) THEN hug_lib$ = CONCAT $("libglib-2.0.so.", STR $(hug_sequence))
IMPORT "g_timeout_add(int,void*,long)" FROM hug_lib$ TYPE int

RETURN hug_lib$

LABEL Print_Error
PRINT "Glib library not found!"
END

END FUNCTION

REM --------------------------------------------------------------------------------------------------

FUNCTION hug_Get_Gobject$ : REM INCLUDE

LOCAL hug_lib$
LOCAL hug_sequence = -1

IF INSTR (OS$, "Darwin") THEN
hug_lib$ = "/opt/local/lib/libgobject-2.0.0.dylib"
IF NOT (FILEEXISTS (hug_lib$)) THEN
hug_lib$ = "/Library/Frameworks/Glib.framework/Libraries/libgobject-2.0.0.dylib"
END IF
END IF

CATCH GOTO Redo_Import

LABEL Redo_Import
INCR hug_sequence
IF hug_sequence = HUG_MAX_LIB_SEQ THEN GOTO Print_Error
IF NOT (INSTR (hug_lib$, "dylib")) THEN hug_lib$ = CONCAT $("libgobject-2.0.so.", STR $(hug_sequence))
IMPORT "g_object_unref(long)" FROM hug_lib$ TYPE void

RETURN hug_lib$

LABEL Print_Error
PRINT "Gobject library not found!"
END

END FUNCTION

Last edited by big_bass on Sun 09 Oct 2011, 04:41, edited 9 times in total.

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#2 Post by big_bass »

so how is it done?

I wrote a few simple scripts
to break it down into steps so its easier to modify
the secret always is keep it simple

*wrote another one with sed but "replace" works better and easier to read
the code using sed it looks cryptic


colorlist gets auto generated it is a dragNdrop script
it WILL modify original files so make a copy of the file first
by renaming your original file

in the example above I used hug.bac and renamed it to hug.bac1
then dagNdrop hug.bac1 onto /tmp/colorlist

if you need the replace package
http://www.puppy2.org/slaxer/replace-2. ... 1_SLXR.tgz

for more info or if you want to compile it
http://replace.richardlloyd.org.uk/

Code: Select all

# Joe Arose 10-6-2011
#!/bin/bash
# call this   myparser
# custom keyword lexer to add color to Bacon code 
# the export format is phpbb code to allow color to code in forum 
# posts 

# this reads KEYWORD.TXT
# that contains all BaCon and HUG keywords
# this is a bash version  keyword lexer that is designed to apply
# keyword custom color highlighting for forum posts
# using phpbb code [color=blue]$word[/color]

 
# then replace is used to generate a phpbb code script called 
#/tmp/colorlist_generate 
# which will take its input from dragNdrop 

# you must have the replace package installed http://replace.richardlloyd.org.uk/

mkdir -p /root/mylexer
> /tmp/colorlist 
for i in  `cat /root/mylexer/KEYWORD.TXT` ; do
        echo " replace -e \"$i\" \"[color=blue]$i[/color] \" \"\$@\" ">> /tmp/colorlist   
done  
Attachments
colorlist.tar.gz
(2.04 KiB) Downloaded 230 times
KEYWORD.TXT.tar.gz
(1.04 KiB) Downloaded 224 times

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#3 Post by big_bass »

now with two colors
UPDATED code Oct-9-20011
and added a readme

BaCon in blue
HUG in green

doesnt that look so much better with color ?


remember to inclose using the forums Quote
this allows colors to be used the Code doesnt work with colors

' Imports when HUG is used as shared object
CONST HUG_lib$ = "./hug.so"

' Setup error handling
TRAP LOCAL
CATCH GOTO hug_handle_error

' Import HUG functions
IMPORT "ATTACH(long,long,int,int)" FROM HUG_lib$ TYPE void
IMPORT "BUTTON(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "CALLBACK(long,void*)" FROM HUG_lib$ TYPE void
IMPORT "CALLBACKX(long,void*,long)" FROM HUG_lib$ TYPE void
IMPORT "CANVAS(int,int)" FROM HUG_lib$ TYPE long
IMPORT "CHECK(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "CIRCLE(char*,int,int,int,int,long)" FROM HUG_lib$ TYPE void
IMPORT "CLIPBOARD" FROM HUG_lib$ TYPE long
IMPORT "COMBO(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "DISABLE(long)" FROM HUG_lib$ TYPE void
IMPORT "DISPLAY" FROM HUG_lib$ TYPE void
IMPORT "EDIT(int,int)" FROM HUG_lib$ TYPE long
IMPORT "ENABLE(long)" FROM HUG_lib$ TYPE void
IMPORT "ENTRY(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "FILEDIALOG(char*,char*,int,int,int)" FROM HUG_lib$ TYPE long
IMPORT "FOCUS(long)" FROM HUG_lib$ TYPE void
IMPORT "FONT(long,char*)" FROM HUG_lib$ TYPE void
IMPORT "FRAME(int,int)" FROM HUG_lib$ TYPE long
IMPORT "GET(long)" FROM HUG_lib$ TYPE long
IMPORT "GRAB$(long)" FROM HUG_lib$ TYPE char*
IMPORT "HIDE(long)" FROM HUG_lib$ TYPE void
IMPORT "HSEPARATOR(int)" FROM HUG_lib$ TYPE long
IMPORT "HUGOPTIONS(char*)" FROM HUG_lib$ TYPE void
IMPORT "HUGLIB$(char*)" FROM HUG_lib$ TYPE char*
IMPORT "IMAGE(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "INIT" FROM HUG_lib$ TYPE void
IMPORT "KEY" FROM HUG_lib$ TYPE long
IMPORT "LINE(char*,int,int,int,int)" FROM HUG_lib$ TYPE void
IMPORT "LIST(int,int)" FROM HUG_lib$ TYPE long
IMPORT "MARK(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "METHOD(long,int,void*)" FROM HUG_lib$ TYPE void
IMPORT "MOUSE(long)" FROM HUG_lib$ TYPE long
IMPORT "MSGDIALOG(char*,int,int,int,int)" FROM HUG_lib$ TYPE long
IMPORT "NOTEBOOK(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "OUT(char*,char*,char*,int,int)" FROM HUG_lib$ TYPE void
IMPORT "PASSWORD(int,int)" FROM HUG_lib$ TYPE long
IMPORT "PICTURE(char*,int,int,int,int)" FROM HUG_lib$ TYPE void
IMPORT "PIXEL(char*,int,int)" FROM HUG_lib$ TYPE void
IMPORT "PROGRESSBAR(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "QUIT" FROM HUG_lib$ TYPE void
IMPORT "RADIO(char*,int,int,long)" FROM HUG_lib$ TYPE long
IMPORT "REGISTER(long,int,int,int,long,long,long,long)" FROM HUG_lib$ TYPE void
IMPORT "SCREENSIZE(int)" FROM HUG_lib$ TYPE int
IMPORT "SET(long,int)" FROM HUG_lib$ TYPE void
IMPORT "SHOW(long)" FROM HUG_lib$ TYPE void
IMPORT "SPIN(int,int,double,double,double)" FROM HUG_lib$ TYPE long
IMPORT "SQUARE(char*,int,int,int,int,int)" FROM HUG_lib$ TYPE void
IMPORT "STOCK(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "SYNC" FROM HUG_lib$ TYPE void
IMPORT "TEXT(long,char*)" FROM HUG_lib$ TYPE void
IMPORT "TIMEOUT(int,void*)" FROM HUG_lib$ TYPE void
IMPORT "TOGGLE(char*,int,int)" FROM HUG_lib$ TYPE long
IMPORT "UNFOCUS(long)" FROM HUG_lib$ TYPE void
IMPORT "VSEPARATOR(int)" FROM HUG_lib$ TYPE long
IMPORT "WINDOW(char*,int,int)" FROM HUG_lib$ TYPE long

' Define the PROPERTY directive
hug_Gobject$ = HUGLIB$("gobject")
IMPORT "g_object_set(long,char*,...)" FROM hug_Gobject$ TYPE void
PROTO PROPERTY
DEF FN PROPERTY(widget, name, ...) = g_object_set(widget, name, __VA_ARGS__, NULL)

' The dialog response codes
CONST GTK_RESPONSE_ACCEPT = -3
CONST GTK_RESPONSE_DELETE_EVENT = -4
CONST GTK_RESPONSE_OK = -5
CONST GTK_RESPONSE_CANCEL = -6
CONST GTK_RESPONSE_CLOSE = -7
CONST GTK_RESPONSE_YES = -8
CONST GTK_RESPONSE_NO = -9
CONST GTK_RESPONSE_APPLY = -10
CONST GTK_RESPONSE_HELP = -11


Attachments
bacon-highlighting.tar.gz
(2.12 KiB) Downloaded 245 times
Last edited by big_bass on Mon 10 Oct 2011, 02:53, edited 2 times in total.

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#4 Post by big_bass »

I updated the above post

and this is the readme to get an idea how simple it is


the folder $HOME/bacon-highlighting/ is used
$HOME on puppy is /root

1.)if you click on /root/bacon-highlighting/myparser-bacon
it will auto generate colorlist-bacon
this reads the KEYWORD2.TXT which are BaCon keywords in blue


2.)if you click on/root/bacon-highlighting/myparser-hug
it will auto generate colorlist_hug
this reads the KEYWORDS_HUG.TXT which are HUG keywords in green

*the reason this is auto generated is it allows you to modify it easily
having separate scripts allows for easy modifications and debugging
and as more colors are added this simplifies making color edits to
complex scripts

the file hug.bac was renamed to hug.bac1 just for an example file
any file could be used it is not needed for the script to work

3.)you dragNdrop hug.bac1 onto colorlist-bacon that gives the blue color
4.)you dragNdrop hug.bac1 onto colorlist_hug that gives you the green color


these files were used during testing of the color
if you need hug.bac its here ---------->http://www.basic-converter.org/hug.bac
if you need hug_imports.bac its here ----------->http://www.basic-converter.org/hug_imports.bac

Post Reply