newLISP-TK with syntax highlighting

Started by HPW, January 28, 2006, 08:05:21 AM

Previous topic - Next topic

HPW

Inspired from:



http://www.alh.net/newlisp/phpbb/viewtopic.php?t=941">http://www.alh.net/newlisp/phpbb/viewtopic.php?t=941



I gave it a try and get it working with a few changes to ctext:



http://www.hpwsoft.de//anmeldung//html1//newLISP//ctext.png">



The line numbering is a little side effect! ;-)



Now is there a list what commands are grouped together in what color?
Hans-Peter

pjot

#1
Nice! I see my GTK version gets competition :-)



For the syntax highlighting I used more or less the VIM scheme. So texts between double quotes are highlighted, keywords, and parenthesis. VIM also highlights numbers.



Peter

HPW

#2
ctext has the following highlight commands:


Quote
ctext::addHighlightClass $txt commands blue [list set setq list ]
  • #ctext::addHighlightClass $txt flags color [list ???]

    #ctext::addHighlightClassWithOnlyCharStart $txt vars color "$"

    #ctext::addHighlightClassForSpecialChars $txt brackets green {[]{}}

    #ctext::addHighlightClassForRegexp $txt paths lightblue {.[a-zA-Z0-9_-]+}



  • So I start with the first one.

    Another idea is to put the keywords in groups with their own color.

    That could happen external in newlisp-tk.config so:



    set Ide(newLISPCommands) [list set setq list .....]

    set Ide(newLISPControl) [list if while for .....]
    Hans-Peter

    pjot

    #3
    Hm, grouping commands will maybe lead you into problems? There are commands which can be used for strings, but also for lists...? So which criteria you have to use?



    How does the TK version highlight the syntax anyway? It seems a function within TCL/TK itself? In the GTK version I have to perform the low-level coloring myself.



    Peter

    HPW

    #4
    Quote
    How does the TK version highlight the syntax anyway? It seems a function within TCL/TK itself?


    TCL/TK has grown over years and has a lot of extensions.

    ctext is a megawidget and part of TKlib.



    http://tcllib.sourceforge.net/doc/ctext.html">http://tcllib.sourceforge.net/doc/ctext.html
    Hans-Peter

    HPW

    #5
    A bit more work:



    ;Helperlisp for getting keywords

    (device (open "keywords.txt" "w"))
    (dolist (s (symbols)) (if (primitive? (eval s)) (println s)))
    (close (device))

    (device (open "keywords.tcl" "w"))
    (setq commandstring "set Ide(newLISPCommands) [list ")
    (dolist (s (symbols)) (if (primitive? (eval s)) (setq commandstring (string commandstring s " "))))
    (setq commandstring (string commandstring "]"))
    (println commandstring)
    (close (device))


    You get:

    set Ide(newLISPCommands) [list ! != $ % & * + - / < << <= = > >= >> NaN? ^ abs ..... ]



    Put in newlsip-tk.config:
    Quote
    set Ide(newLISPCommands) [list ! != $ % & * + - / < << <= = > >= >> NaN? ^ abs ..... ]

    set Ide(CommandColor) blue

    set Ide(StringColor) orange

    set Ide(CommentColor) turquoise4


    Using now in newlisp-tk.tcl:

    ctext::addHighlightClass $txt commands $Ide(CommandColor) $Ide(newLISPCommands)
    ctext::addHighlightClassForSpecialChars $txt paranthesis red {()}
    ctext::addHighlightClassForSpecialChars $txt brackets darkred {[]}
    ctext::addHighlightClassForRegexp $txt dstrings $Ide(StringColor) {".*"}
    ctext::addHighlightClassForRegexp $txt cstrings $Ide(StringColor) {{.*}}
    ctext::addHighlightClassForRegexp $txt tstrings $Ide(StringColor) {[text].*[/text]}
    ctext::addHighlightClassForRegexp $txt comments $Ide(CommentColor) {;.*}


    http://www.hpwsoft.de//anmeldung//html1//newLISP//ctext1.png">



    Other ideas for regexpressions for highlighting?



    Modified sources (TK V1.34?) here:



    http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip">http://www.hpwsoft.de//anmeldung//html1 ... Khighl.zip">http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip
    Hans-Peter

    HPW

    #6
    Added ctext to the editor:



    http://www.hpwsoft.de//anmeldung//html1//newLISP//ctext2.png">



    Don't know why line 1 is not highlighting correctly on a fresh view.

    When I edit it then it does it.



    I updated:

    http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip">http://www.hpwsoft.de//anmeldung//html1 ... Khighl.zip">http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip
    Hans-Peter

    HPW

    #7
    I added to the newlisp-tk.config:
    Quote
    set Ide(ConsoleLineMap) 1

    set Ide(EditLineMap) 0

    Enable/Disable the linemap in console and editor.



    (As a workaround I add a n to the editor-content-begin. Then highlighting does work on the whole content)

    There still is a problem with the initial focus, which does not work as before.

    I updated:

    http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip">http://www.hpwsoft.de//anmeldung//html1 ... Khighl.zip">http://www.hpwsoft.de//anmeldung//html1//newLISP//newlisptkhighl.zip
    Hans-Peter

    Lutz

    #8
    Very nice! This could go into one of the next versions. Is ctext part of the normal Tcl/Tk 8.4 library? I mean do I get it with the Freewrap distribution? Or would it be an add-on like BWidget?



    Lutz

    HPW

    #9
    Quote
    Is ctext part of the normal Tcl/Tk 8.4 library?


    Not absolute sure.

    TKlib 0.4 is part of the ActiveState free TCL-distribution.

    Freewrap 6.2 states that it is based on 8.4.12 (latest version).


    Quote
    I mean do I get it with the Freewrap distribution? Or would it be an add-on like BWidget?


    It could be so. Give it a simple try first.

    Anyway it should be easy since it only is a 24 KB pure tcl source.



    But there are still the 2 problems with:

    Not highlighting first line in editor (my workaround isn't really good)

    Initial focus not in console/editor window.



    Without that it could get real nice.
    Hans-Peter

    newdep

    #10
    TKlib is a standallown package and not part of the default TclTK distribution.

    Though indeed ActiveState links it (as it links all kinds of tk stuff with its distribitions)..



    But I think it should not be too difficult to create yours own

    Highlighting "widget" inside tk.. (perhaps wiki.tk has some topics..)





    Or!! even better.. Rip-out the Ctext part from the TKLib and distribute it

    with the newlisp-tk version.. that better i think..



    Btw...Nice going HPW ;-)
    -- (define? (Cornflakes))

    Lutz

    #11
    On MacOS X 10.4 it seems to be part of the standard install and is just one file of about 24k: /System/Library/Tcl/tklib0.2/ctext/ctext.tcl



    I guess it will work everywhere, not only MacOS X.



    Lutz