Custom syntax highlighting

100 views
Skip to first unread message

Eloi Torrents

unread,
Feb 12, 2021, 1:14:54 PM2/12/21
to leo-editor
Hi all! 
I discovered Leo yesterday and I find it amazing! I want to use it to write code in a language that it is not supported. 
I think that highlighting the key words of the language would be a good place to start. How should I get started? (I would like to know the required configuration and where should I place it).

I found http://leoeditor.com/slides/customizing.html#writing-new-importers, but this is not very friendly to begin, and also I'm not sure if this is what I need.

Thanks
Eloi

Edward K. Ream

unread,
Feb 13, 2021, 7:17:43 AM2/13/21
to leo-editor
On Fri, Feb 12, 2021 at 12:14 PM Eloi Torrents <eloi.to...@gmail.com> wrote:

I discovered Leo yesterday and I find it amazing! I want to use it to write code in a language that it is not supported. 
I think that highlighting the key words of the language would be a good place to start. How should I get started? (I would like to know the required configuration and where should I place it).

Start here. Thanks for your interest in improving Leo.

Welcome aboard. Please feel free to ask any questions you may have, on whatever topic.

Edward

Eloi Torrents

unread,
Feb 15, 2021, 2:37:26 PM2/15/21
to leo-editor
Thanks for the response! It was enough for getting me starting :) I already defined a couple of parsing rules and they work as expected. Right now I have some questions:

 - I try to use "@color comment1 = blue" and it seems to do nothing. I tried putting "@language python" at the top of the file, and a python comment "#like this". Then I put "@color comment1 = blue" at the root node of the tree, I also putted it as a subnode of the @settings node and also in mySettings.leo. It doesn't work even when I restart Leo. :( What am I doing wrong?

- Maybe this is related. I was reading the modes/python.py file, and I was looking at python_rule20. This uses match_mark_previous, which I think that searches for "(" and colorizes whatever comes before in the "function" color. I don't understand why It doesn't color the "def init1():" line in the example in "quickstart.leo", under Programming>External files...

Other non-related question that I have:
 Is it possible to import recursively the files of a folder? I tried "@path" and right clicking
 "import files", but I have to select all the files manually and it is very annoying. Is there a better way? This also puts "@language unknown_language" at the top of each file... How can I make it detect my language?

Thank you very much!

Eloi

Edward K. Ream

unread,
Feb 16, 2021, 9:51:13 AM2/16/21
to leo-editor
On Mon, Feb 15, 2021 at 1:37 PM Eloi Torrents <eloi.to...@gmail.com> wrote:

 - I try to use "@color comment1 = blue" and it seems to do nothing.

There is no @color directive. Instead, put the @color setting in myLeoSettings.leo. Does this make sense?

- Maybe this is related. I was reading the modes/python.py file, and I was looking at python_rule20. This uses match_mark_previous, which I think that searches for "(" and colorizes whatever comes before in the "function" color. I don't understand why It doesn't color the "def init1():" line in the example in "quickstart.leo", under Programming>External files...

Try running Leo (from a console) with --trace-coloring. This superb trace shows exactly what rules Leo's colorizer invokes.  Btw, to get a list of all possible traces, do leo -h.

Is it possible to import recursively the files of a folder?

Yes. Execute this script from inside Leo:

dir_ = r'<path to the root directory>'
c.recursiveImport(
    dir_ = dir_,
    kind = '@clean', # '@clean', #'@nosent','@auto','@file',
    recursive = True,
    safe_at_file = False,
    theTypes = ['.py',]
)

When True, safe_at_file imports changes @ to @@ for all imported at-file nodes. I use safe_at_file for study, but if I'm actually going to use the files I set safe_at_file = False as shown.

theTypes is a list of file extensions to import. You can import almost any type, but not '.leo'.

If you care about only one file, you can set dir_ to the full path to that one file.

Edward

Eloi Torrents

unread,
Feb 16, 2021, 3:54:01 PM2/16/21
to leo-editor
Thank you!

Running leo --trace=coloring in the example class "eggs" in the file quickstart.leo I got:

mainLoop NEW NODE: state 1 = None eggs

setTag: python.keyword1             0   3                'def' python_main:python_rule21
setTag: python.keyword1             4   8               'pass' python_main:python_rule21

This seams as if it ignores completely python_rule20... I might miss something...

389  │ def python_rule20(colorer, s, i):
390  │     return colorer.match_mark_previous(s, i, kind="function", pattern="(",
391  │         at_line_start=False, at_whitespace_end=False, at_word_start=False, exclude_match=True)

Shouldn't this detect the "(" , and then match backwards? I also tried exclude_match=False, but the result was the same.

Eloi

Edward K. Ream

unread,
Feb 16, 2021, 4:03:40 PM2/16/21
to leo-editor


On Tue, Feb 16, 2021 at 2:54 PM Eloi Torrents <eloi.to...@gmail.com> wrote:
Thank you!

You're welcome. My pleasure.

Running leo --trace=coloring in the example class "eggs" in the file quickstart.leo I got:

mainLoop NEW NODE: state 1 = None eggs

setTag: python.keyword1             0   3                'def' python_main:python_rule21
setTag: python.keyword1             4   8               'pass' python_main:python_rule21

This seams as if it ignores completely python_rule20... I might miss something...

Searching for `def match_mark_previous`  finds:

def match_mark_previous(self, s, i,
    kind='', pattern='',
    at_line_start=False, at_whitespace_end=False, at_word_start=False,
    exclude_match=False
):
   ...
    # This match was causing most of the syntax-color problems.
    return 0  # 2009/6/23

I don't remember any details. This was before the days of PR's, so there aren't any easily accessible notes. You could go investigate using gitk. But for now, you can see that this matcher has been killed.

HTH.

Edward

Eloi Torrents

unread,
Feb 16, 2021, 4:51:05 PM2/16/21
to leo-editor
Thank you, then this is clear :)

I think it would be good to write something about it here https://leoeditor.com/coloring.html#match-mark-previous and maybe deleting any reference to it in the modules/x.py files... I was taking them as examples and it is very confusing to have rules that do nothing. :)

If needed the same effect can be obtained using regexs, I think that the performance cost will be unnoticeable.

Eloi

Eloi Torrents

unread,
Feb 16, 2021, 5:37:06 PM2/16/21
to leo-editor
By the way, at last I found a way to change the colors: I needed to use "@color comment1-color = blue". In the reference it said that the "-colors" is optional:

> Note: all @color settings may optionally end with the _color suffix.

On the other hanf, if match_mark_previous won't be back, I think that I can help changing the x.py files :)

Eloi

Edward K. Ream

unread,
Feb 17, 2021, 1:32:46 PM2/17/21
to leo-editor
On Tue, Feb 16, 2021 at 3:51 PM Eloi Torrents <eloi.to...@gmail.com> wrote:
Thank you, then this is clear :)

I think it would be good to write something about it here https://leoeditor.com/coloring.html#match-mark-previous and maybe deleting any reference to it in the modules/x.py files... I was taking them as examples and it is very confusing to have rules that do nothing. :)

Good ideas. The code will be quicker without the do-nothing patterns.

If needed the same effect can be obtained using regexs, I think that the performance cost will be unnoticeable.

Regexs almost certainly will work.  The performance cost of any reasonable pattern will be negligible.

Edward

Edward K. Ream

unread,
Feb 17, 2021, 1:33:48 PM2/17/21
to leo-editor
On Tue, Feb 16, 2021 at 4:37 PM Eloi Torrents <eloi.to...@gmail.com> wrote:
 
if match_mark_previous won't be back, I think that I can help changing the x.py files :)

Any help will be appreciated. You've already found something that should be fixed.

Edward

Edward K. Ream

unread,
Feb 18, 2021, 2:11:53 PM2/18/21
to leo-editor
On Tue, Feb 16, 2021 at 3:51 PM Eloi Torrents <eloi.to...@gmail.com> wrote:
Thank you, then this is clear :)

I think it would be good to write something about it here https://leoeditor.com/coloring.html#match-mark-previous and maybe deleting any reference to it in the modules/x.py files... I was taking them as examples and it is very confusing to have rules that do nothing. :)

I have just created #1821 for this. I'll get to this fairly soon.

Edward
Reply all
Reply to author
Forward
0 new messages