Skip to content
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

Full inferior mode input font locking #1428

Closed
memeplex opened this issue Aug 2, 2018 · 6 comments · May be fixed by #1698
Closed

Full inferior mode input font locking #1428

memeplex opened this issue Aug 2, 2018 · 6 comments · May be fixed by #1698

Comments

@memeplex
Copy link

memeplex commented Aug 2, 2018

I know this is a comint issue and I've already reported at https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32344 (please see details there).

But maybe you're interested in adding this workaround to elpy (perhaps optionally depending on a customization option) in order to get full input font locking, it looks really nice!

(advice-add 'comint-send-input
            :around (lambda (f &rest args)
                      (when (eq major-mode 'inferior-python-mode)
                        (cl-letf (((symbol-function 'g)
                                   (symbol-function 'add-text-properties))
                                  ((symbol-function 'add-text-properties)
                                   (lambda (start end properties &optional object)
                                     (unless (eq (nth 3 properties) 'comint-highlight-input)
                                       (g start end properties object)))))
                          (apply f args)))))
@memeplex
Copy link
Author

memeplex commented Aug 2, 2018

Here is an improvement on the above code:

  1. Uses a safer let binding for g (still safer if you use lexical binding).
  2. Also colorizes the input send by elpy from another buffer.
(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)
    (if (not no-font-lock)
        (if (eq face 'comint-highlight-input)
            (python-shell-font-lock-post-command-hook)
          (add-text-properties from-point (point)
                               (list 'front-sticky t 'font-lock-face face))))))

(advice-add 'comint-send-input
            :around (lambda (f &rest args)
                      (when (eq major-mode 'inferior-python-mode)
                        (cl-letf ((g (symbol-function 'add-text-properties))
                                  ((symbol-function 'add-text-properties)
                                   (lambda (start end properties &optional object)
                                     (unless (eq (nth 3 properties) 'comint-highlight-input)
                                       (funcall g start end properties object)))))
                          (apply f args)))))

Notice I had to redefine elpy-shell--insert-and-font-lock in order to avoid the standard comint input face and also to call the python font locking hook.

I understand all this is a workaround but the results look really nice! I suggest elpy to implement the above but in a more cleanly fashion:

  1. The comint-send-input advice could be kept until the comint maintainers decide to add an option to avoid forced colorization of inputs (or forever if they decide not to do so).

  2. Everything should depend on a new customization toggle `elpy-shell-input-font-lock' or whatever. When this toggle is enabled the advice is installed and elpy-shell--insert-and-font-lock calls python-shell-font-lock-post-command-hook, otherwise behavior is as usual.

What do you think?

(And please, give it a try, I believe once you saw the colors in your comint buffer you would want to add this!)

@memeplex
Copy link
Author

memeplex commented Aug 2, 2018

The above was intended to suggest how this could be added as part of elpy. If anyone is wanting to hack his/her config right now, I would recommend to advice elpy-shell--insert-and-font-lock instead of overwriting it:

(advice-add 'elpy-shell--insert-and-font-lock
            :around (lambda (f string face &optional no-font-lock)
                      (if (not (eq face 'comint-highlight-input))
                          (funcall f string face no-font-lock)
                        (funcall f string face t)
                        (python-shell-font-lock-post-command-hook))))

@memeplex memeplex changed the title Better inferior mode input font locking Full inferior mode input font locking Aug 2, 2018
@memeplex
Copy link
Author

memeplex commented Aug 2, 2018

@jorgenschaefer
Copy link
Owner

Thank you for the suggestion! I like the idea, but I would prefer not having to maintain this code in the future. Having it in comint would be great, in the meantime, having it in the wiki is a good workaround. Thanks again! :-)

@memeplex
Copy link
Author

memeplex commented Oct 13, 2019

@galaunay I'm trying to push the required changes into comint so as we can have full font locking in inferior shell, maybe you could express your interest in that feature in https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32344. Thanks.

@galaunay
Copy link
Collaborator

I would ne very pleased for this to be merged as well.
I'll send a message (if it can by of any help).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants