View Single Post
Old 10-10-2022, 10:13 AM   #36
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,489
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by un_pogaz View Post
And after a night's sleep, I just realized the inconsistency of using the arguments for python:.

*inhale, sigh*

In fact, I think I even misunderstood the purpose of python template (it's an alternative form to Stored, not a 3rd option totally inedite).

If you think that deleting them would improve the code, go ahead.
I will leave it. Reasoning:
  1. We should have a way to save and reuse python templates. It could be done with files but that seems a bit crude.
  2. "Stored templates" already exists, so why not reuse it?
  3. Stored GPM templates are callable with arguments.
  4. Therefore stored python templates should also be callable.
In addition there is a bona fide reason to have arguments in stored python templates: template searches. A person could write a template that does some sort of search, but with variants. Instead of duplicating the code, store the template and call it with arguments that tell it which variant to use.

All this opens the question "What is the difference between a python template and a python formatter function? Good question. Formatter functions can "see" into the formatter and easily use existing formatter builtin functions, but templates cannot. If I had thought of python templates years ago perhaps I wouldn't have built the user-defined function mechanism. But we have it now and it is used, so it can't go away or be changed.
Quote:
Also, I was going to say that syntax highlighting is possible, since "Action Chains" does it in its module editor... but it uses something different (TextEdit from calibre.gui2.tweak_book.editor.text), so no, crap.

At least, maybe highlight python: to identify what we are in.
... Maybe an idea, I'm looking at it.

EDIT: And, it works.
Spoiler:
Well, really small bug, the highlighting of the key words of the modes is made at the beginning of each new line whereas normally it should be exclusively at the beginning of the full text. Detail, really. I won't have to think about it here, I'll wait until it's pushed to your github or to the calibre github to take the time to make a clean commit (if possible).
I had already taken the code to do python syntax highlighting from gui2.widgets.PythonHighlighter and integrated it into the highlighter used by the template dialog. It works quite well. The difficulty was dynamically switching between highlighters.

I didn't solve the problem of highlighting the mode indicator.

EDIT: Attached is template_dialog.py with python syntax highlighting.
Attached Files
File Type: zip template_dialog.zip (10.0 KB, 287 views)

Last edited by chaley; 10-10-2022 at 10:15 AM.
chaley is offline   Reply With Quote