You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IDLE: allow syntax highlighting of files without .py extension #51107
I'm +1 on adding such an option to the menu, if only to be able to highlight files including only Python code which don't happen to have the .py extension.
If support for files including not only Python code is important to anyone, some examples of such files would be very helpful. I've never seen such files and I don't know how common such uses are.
Supporting files with some Python code in between other stuff is a different matter. Non-Python code being wrongly highlighted may be fine, but we'll need to make sure that the syntax highlighting is robust enough to be able to withstand going through a lot of invalid syntax, and still correctly highlight most of the Python code found in between. If IDLE fails to highlight a file it could of course fail gracefully, give an error message and not highlight, so I don't foresee this causing any stability issues.
I'm +1 on adding such an option to the menu, if only to be able to highlight files including only Python code which don't happen to have the .py extension.
If support for files including not only Python code is important to anyone, some examples of such files would be very helpful. I've never seen such files and I don't know how common such uses are.
Really, docs.python.org is full of examples with python code in it :)
Supporting files with some Python code in between other stuff is a different matter. Non-Python code being wrongly highlighted may be fine, but we'll need to make sure that the syntax highlighting is robust enough to be able to withstand going through a lot of invalid syntax, and still correctly highlight most of the Python code found in between. If IDLE fails to highlight a file it could of course fail gracefully, give an error message and not highlight, so I don't foresee this causing any stability issues.
There are, perhaps 3 main issues with new features;
What is the specific proposal?
In particular, where should the new menu entry go and what should it say.
Will it hurt anything?
Will it be useful.
As to these, I suggest you try your own experiment. Rename x.html to x.py and pull into the editor.
Doing that with controlflow.html shows that this appears to be harmless. For this file, it is also useless:
a. My WinXP32 3.1.2 IDLE edit window does not have a horizontal scroll bar. It does not need one for <80 char lines. It also does not do autowrap, as that would be fatal for code. However, this file has some lines with a few hundred chars and there is no easy way to read them.
b. The html for Python code is not always what you see in a browser. Here is one line from your example: "<span class="k">print</span> <span class="n">x</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>" (no wrapping in the original).
I think Tal's case of .py-less Python files is a reason to add a menu entry, and let the user beware with anything else.
There are, perhaps 3 main issues with new features;
What is the specific proposal?
In particular, where should the new menu entry go and what should it say.
edit -> syntax highlight
Will it hurt anything?
no
Will it be useful.
yes
As to these, I suggest you try your own experiment. Rename x.html to x.py and pull into the editor.
Doing that with controlflow.html shows that this appears to be harmless. For this file, it is also useless:
In this case useless it is. But try imagining some more <pre></pre>
friendly html. Would it not be asome if you could craft a file that
opens with your browser to see what it does before you run it with
python?
a. My WinXP32 3.1.2 IDLE edit window does not have a horizontal scroll bar. It does not need one for <80 char lines. It also does not do autowrap, as that would be fatal for code. However, this file has some lines with a few hundred chars and there is no easy way to read them.
I can live with arrow keys
b. The html for Python code is not always what you see in a browser. Here is one line from your example: "<span class="k">print</span> <span class="n">x</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>" (no wrapping in the original).
I think Tal's case of .py-less Python files is a reason to add a menu entry, and let the user beware with anything else.
Not a warning message please. Just put it off by default on non py
files, like it currently does. Hilighting is harmless and does not
change the contents of the file.
Regarding the warning message, I was referring only to the case that the syntax highlighter fails completely and raises an exception. I'm not even sure if that can happen, but in case it does an informative message would be better than just not highlighting, and much better than IDLE dying. I'm bringing this up in this context because IMO if we allow highlighting of arbitrary files, we should at least ensure that this doesn't crash IDLE if the highlighting does fail.
serwy commented on Jul 12, 2012
serwymannequin
Mannequin
More actions
Attached is a patch to allow toggling of the ColorDelegator. It can be toggled using the existing, but undocumented, Control-/ binding.
The patch modifies the "toggle_colorize_event" to fully remove coloring or fully recolorize the text rather than its existing behavior of disabling/enabling the coloring "thread." It also ensures that a ColorDelegator instance exists in every editor window instead of only those where ispythonsource returns True.
Toggling also works in the shell, but turning highlighting off fails if you start typing again. This is due to bpo-13495.
The crashing of IDLE described by Tal is likely due to pythonw.exe and is addressed in bpo-13582.
Roger's patch LGTM, and should be considered a bug-fix.
I will note, however, that this doesn't address the original issue: It still does not supply an easily discoverable method of toggling highlighting. Having a keyboard shortcut documented in the help file is necessary, but few novice users will ever notice it.
There exists a <<toggle-auto-coloring>> bound (at least on Windows) to ^/. When toggled off, it does not removes existing highlighting. So accidentally hitting the key has no immediate visible effect (see bpo-27170 for problem this caused someone.) It does re-highlight existing code when toggled on. The turn off part should be fixed and maybe a status bar message given.
It only applies to .py files. With a .txt, it selects the entire file, including the trailing blank (which ^A omits). For this issue, it should be fixed to allow for any file, just starting off for non .py files.
It is one of many key bindings not in doc or not on menu. bpo-28097 is about this general issue.
changed the title [-]This is a python file, apply syntax highlighting[/-][+]IDLE: allow syntax highlighting of files without .py extension[/+]on Sep 12, 2016
Activity
gert commentedon Sep 7, 2009
http://groups.google.be/group/comp.lang.python/browse_thread/thread/252fa1ccd0251977#
Menu option please, so I can highlight .wsgi .txt .xml files with python
code in it, does not matter if non python code gets wrongly highlighted too.
terryjreedy commentedon Aug 1, 2010
Interesting idea.
taleinat commentedon Aug 1, 2010
I'm +1 on adding such an option to the menu, if only to be able to highlight files including only Python code which don't happen to have the .py extension.
If support for files including not only Python code is important to anyone, some examples of such files would be very helpful. I've never seen such files and I don't know how common such uses are.
Supporting files with some Python code in between other stuff is a different matter. Non-Python code being wrongly highlighted may be fine, but we'll need to make sure that the syntax highlighting is robust enough to be able to withstand going through a lot of invalid syntax, and still correctly highlight most of the Python code found in between. If IDLE fails to highlight a file it could of course fail gracefully, give an error message and not highlight, so I don't foresee this causing any stability issues.
taleinat commentedon Aug 1, 2010
In the last paragraph of my previous post, instead of "is a different matter" read "may be problematic".
gert commentedon Aug 1, 2010
On Sun, Aug 1, 2010 at 4:55 PM, Tal Einat <report@bugs.python.org> wrote:
Really, docs.python.org is full of examples with python code in it :)
Try to parse this html file
(http://docs.python.org/tutorial/controlflow.html) for testing.
terryjreedy commentedon Aug 1, 2010
There are, perhaps 3 main issues with new features;
In particular, where should the new menu entry go and what should it say.
As to these, I suggest you try your own experiment. Rename x.html to x.py and pull into the editor.
Doing that with controlflow.html shows that this appears to be harmless. For this file, it is also useless:
a. My WinXP32 3.1.2 IDLE edit window does not have a horizontal scroll bar. It does not need one for <80 char lines. It also does not do autowrap, as that would be fatal for code. However, this file has some lines with a few hundred chars and there is no easy way to read them.
b. The html for Python code is not always what you see in a browser. Here is one line from your example: "<span class="k">print</span> <span class="n">x</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>" (no wrapping in the original).
I think Tal's case of .py-less Python files is a reason to add a menu entry, and let the user beware with anything else.
gert commentedon Aug 1, 2010
On Mon, Aug 2, 2010 at 12:06 AM, Terry J. Reedy <report@bugs.python.org> wrote:
edit -> syntax highlight
no
yes
In this case useless it is. But try imagining some more <pre></pre>
friendly html. Would it not be asome if you could craft a file that
opens with your browser to see what it does before you run it with
python?
I can live with arrow keys
Not a warning message please. Just put it off by default on non py
files, like it currently does. Hilighting is harmless and does not
change the contents of the file.
taleinat commentedon Aug 2, 2010
I think we are in agreement :)
Regarding the warning message, I was referring only to the case that the syntax highlighter fails completely and raises an exception. I'm not even sure if that can happen, but in case it does an informative message would be better than just not highlighting, and much better than IDLE dying. I'm bringing this up in this context because IMO if we allow highlighting of arbitrary files, we should at least ensure that this doesn't crash IDLE if the highlighting does fail.
serwy commentedon Jul 12, 2012
Attached is a patch to allow toggling of the ColorDelegator. It can be toggled using the existing, but undocumented, Control-/ binding.
The patch modifies the "toggle_colorize_event" to fully remove coloring or fully recolorize the text rather than its existing behavior of disabling/enabling the coloring "thread." It also ensures that a ColorDelegator instance exists in every editor window instead of only those where ispythonsource returns True.
Toggling also works in the shell, but turning highlighting off fails if you start typing again. This is due to bpo-13495.
The crashing of IDLE described by Tal is likely due to pythonw.exe and is addressed in bpo-13582.
taleinat commentedon Feb 4, 2014
Roger's patch LGTM, and should be considered a bug-fix.
I will note, however, that this doesn't address the original issue: It still does not supply an easily discoverable method of toggling highlighting. Having a keyboard shortcut documented in the help file is necessary, but few novice users will ever notice it.
rhettinger commentedon Aug 10, 2014
I'll try this out in the next couple of weeks in a classroom setting.
terryjreedy commentedon Sep 12, 2016
There exists a <<toggle-auto-coloring>> bound (at least on Windows) to ^/. When toggled off, it does not removes existing highlighting. So accidentally hitting the key has no immediate visible effect (see bpo-27170 for problem this caused someone.) It does re-highlight existing code when toggled on. The turn off part should be fixed and maybe a status bar message given.
It only applies to .py files. With a .txt, it selects the entire file, including the trailing blank (which ^A omits). For this issue, it should be fixed to allow for any file, just starting off for non .py files.
It is one of many key bindings not in doc or not on menu. bpo-28097 is about this general issue.
rhettinger commentedon Sep 12, 2016
+0 from me.
[-]This is a python file, apply syntax highlighting[/-][+]IDLE: allow syntax highlighting of files without .py extension[/+]