-
Notifications
You must be signed in to change notification settings - Fork 258
Use native python fontification when sending input to shell (fix #1428) #1698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Nice, |
Not at all. Maybe @npostavs could make a more informed guess here. |
|
I like this idea! I did a quick try & it did not work in my setup, however. In particular, it seems to break when continuation lines are used ( One way to fix this may be to insert the code fragment into a temporary buffer, propertize it there using the python font lock, and then copying it line by line to the shell buffer. |
|
Have you installed the corresponding comint and python.el patches that should be merged into emacs 27 for this to work? You also have a more hacky version in the wiki using advices. |
|
Yes, it's the call to the python font lock hook that breaks. |
AFAICR this is exactly what is done. The changes here just disable recolorizing the already colorized text. If there is something wrong in the way text is colorized in some corner cases, I believe the problem should be addressed separately. |
|
I apologize, the problem arose because I already applied PR #1710. I now changed the PR slightly so that the advice-based hack from the doc still works. It's not nice, but at least it does not break. That being said, if continuation prompts are turned on and the hack are being used, then the font lock of the continuation prompt is lost (at least in my setup). Also, whether or not continuation prompts are used, the font lock in the shell buffer does not seem to match the one in the Python buffer (e.g., integers and variable assignments are not font-locked in the shell buffer). Is this the same with this PR? One idea that I had when originally writing this code was to keep track of the text properties from the python buffer throughout. I never got to implement this. The advantage would be that the font lock in the shell buffer exactly matches the python buffer. It should also be faster since there is no separate font lock. |
|
The problem with not fontifying numbers arises also directly in the shell buffer (i.e., when entering numbers there); not sure why. |
|
I figured out the reason for the difference in number highlighting: I have highlight-numbers-mode enabled python-mode, and it's not automatically enabled in inferior-python-mode as well. Manually enabling it solves this issue. The only remaining problem is that the fontification of the continuation prompt is destroyed. |
|
Ok, found the issue, fix below. I suggest to change this PR such that only (defun elpy-shell--insert-and-font-lock (string face &optional no-font-lock)
"Inject STRING into the Python shell buffer."
(let ((from-point (point)))
(insert string)
(unless no-font-lock
(if (eq face 'comint-highlight-input)
(cl-letf* ((stp (symbol-function 'set-text-properties))
((symbol-function 'set-text-properties)
(lambda (start end plist &optional object)
(while (< start end)
(unless (eq (get-text-property start 'face) 'comint-highlight-prompt)
(funcall stp start (+ 1 start) plist object))
(setq start (+ 1 start))))))
(python-shell-font-lock-post-command-hook))
(add-text-properties
from-point (point)
(list 'front-sticky t 'font-lock-face face))))))Only the advice for comint-input-prompt is then needed (until addressed upstream). |
|
This fix improves things, but it is still not perfect. Since |
|
I found a simpler and better solution, see PR #1711. Let me know what you think. |
|
#1711 works fine for me. Will we have some compatibility issue if we merge it and the patches to Emacs get merged as well ? |
c4a2564 to
d974e00
Compare
PR Summary
Use full python font locking in shell, even when sending input from an elpy buffer. Fixes #1428
Warning: this is not ready for merging because it requires some changes upstream that I'm pushing here: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32344
Specifically, these two very simple patches should be merged:
https://debbugs.gnu.org/cgi/bugreport.cgi?msg=20;att=1;bug=32344;filename=0001-Option-for-comint-to-honour-original-input-highlight.patch
https://debbugs.gnu.org/cgi/bugreport.cgi?msg=35;att=1;bug=32344;filename=0001-Keep-python-shell-input-colorization-in-comint.patch
Alternatively we could add an "experimental module" (disabled by default) to get this [1] working now using some elisp hack:
But let's hope my patches get merged upstream.
PR checklist
Please make sure that the following things have been addressed (and check the relevant checkboxes):
[1] And probably other stuff that require monkey-patching hacks. Related to #1531