Keyword prefix constraint possible ?



  • Hi all !

    I use N++ for … well … decades ;) . I already defined langages by myself, but this time I’m stuck :
    In GCode, there are commands like G1, G01, M117, G92.1 and so on. Basically speaking, it’s one letter followed by several digits (numbers or dot only, no letters or whatever).
    When I put a letter in the keywords tab (let’s say G for G01…) and check the “prefix” checkbox, it works for G01 and G92.1 which is perfect, but also for Gfoo and Gbar which is not what I want. Is there a way to constrain what can follow the keyword ?
    I tried using the “Number” formating, but I want to have different colors depending on the letter, which is not possible with number which are just … well … numbers ;-)

    Do I have to write a full plugin to do so ?

    I hope I’m clear in my question…



  • @Klasen-Eric said in Keyword prefix constraint possible ?:

    Is there a way to constrain what can follow the keyword ?

    UDL does not inherently have that ability.

    However, if you are willing to install the PythonScript plugin, then you can add extra highlighting to a UDL language using regexes via the script EnhanceUDLLexer.py that @Ekopalypse shares in this linked post – so with multiple regexes, you could color G\d+ green, M\d+ magenta, and so on (color choices are up to you; I based them on the letter of the alphabet).



  • Sounds good ! :D

    I’ll give it a try right now (well, not sure to be efficient, it’s 1:35am here ;) ).



  • This post is deleted!


  • @PeterJones I installed pythonscript, edited the enhance UDL lexer script, but it don’t seem to work… I go to [Plugins] > [Python Script] > [Scripts] > “My Script”

    “My Script” is the modified (copy & paste + update of regex) of the one of your link.

    I tried placing it in the “user” folder (with the new script menu) and by hand in the scripts folder. Both case does not work…

    But nothing happens. I’m sure I’m missing something…



  • @Klasen-Eric ,

    Please go to Plugins > PythonScript and open the Console window after running it: there is likely an error message, which you can share with us



  • @PeterJones Nothing in the console… I tried opening it before running the script or after running it (with restart of N++ ). Nothing…
    Full content of console :

    Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)]
    Initialisation took 32ms
    Ready.
    

    I also modified my code to print something at the beginning, and it works (displays my “Hello World” ;) )



  • @Klasen-Eric ,

    Did you put the name of your UDL in the appropriate spot? Is that UDL active on the active file?



  • @PeterJones I think so. My UDL is just called “gcode”. Quite simple to handle… but I’ll check again… a typo is never too far ;-)

    And yes, this UDL is active on my file (when I change values in its configuration window, colors changes in the file.



  • @PeterJones

    I’ve put some print at the beginning of each method of the code. But even the one at the beginning of the “main” method is not printed. The print at the beginning of the file (just after regex configuration) is still ok.



  • @Klasen-Eric

    So my understanding is, that you run it once, right?
    And after executing you got your main print statement in the console, correct?
    After that, you need to activate the buffer aka clicking into it or switching
    from another document into the one which should be handled and then
    it should start coloring your regex matches.
    Did you ensure that the match group is 0? Only if you used G\d+.
    If you used something like
    G0(\d+) and you are interested to get the digit colored which follow G0
    you would need to use match group 1.
    So for example this

    regexes[(0, (79, 175, 239))] = (r'G\d+', 1)'
    

    would be an error as no match group 1 gets created

    either use this

    regexes[(0, (79, 175, 239))] = (r'G\d+', 0)'
    

    or that (which really doesn’t make sense in this case but …

    regexes[(0, (79, 175, 239))] = (r'(G\d+)', 1)'
    

Log in to reply