Hi Tassilo,

Nice!

The code looks good to me, I'll give it a try later today. Looks like you went a bit further than me to overcome the traps.

On 30 Sep 2015 7:46 am, "Tassilo Horn" <tsdh@gnu.org> wrote:

> Artur Malabarba <bruce.connor.am@gmail.com> writes:
>
> Hi Artur,
>
> >> where `prettify-symbols-compose-at-point' would be the user option.
> >
> > I tried something similar, and unfortunately it's not that simple.
> > Firstly, (window-point) didn't seem to work as expected here, so I had
> > to define a variable in font-lock-mode to hold the value of (point)
> > before fontification started.
> >
> > After doing that, the feature sort of works while you're writing. That
> > is, it doesn't prettify a symbol you've just written, but it does
> > prettify a symbol after you hit SPC, which is nice.  However, it does
> > not work while navigating. That is, when you move point to a
> > prettified symbol, it doesn't get decomposed.
> >
> > Then I tried adding a post-command-hook function to invalidate the
> > font-locking at point. This correctly decomposes a symbol when you
> > move the cursor to it, but it's still not perfect. When you move the
> > cursor *out* of the symbol, if you move far enough, the symbol doesn't
> > get prettified again until you edit something close to it.
>
> Hehe, seems we tried implementing that feature at the same time and both
> fell into the very same traps.  Did you see my other mail?  I have
> gotten it working nicely right yesterday.  Right now I did some minor
> changes, and that's the current version of the patch:
>
> Comments welcome!
>
> --8<---------------cut here---------------start------------->8---
> 1 file changed, 39 insertions(+), 4 deletions(-)
> lisp/progmodes/prog-mode.el | 43 +++++++++++++++++++++++++++++++++++++++----
>
> modified   lisp/progmodes/prog-mode.el
> @@ -29,7 +29,8 @@
>
>  ;;; Code:
>
> -(eval-when-compile (require 'cl-lib))
> +(eval-when-compile (require 'cl-lib)
> +                   (require 'subr-x))
>
>  (defgroup prog-mode nil
>    "Generic programming mode, from which others derive."
> @@ -161,13 +162,20 @@ prettify-symbols--compose-symbol
>    (let ((start (match-beginning 0))
>          (end (match-end 0))
>          (match (match-string 0)))
> -    (if (funcall prettify-symbols-compose-predicate start end match)
> +    (if (and (not (equal prettify-symbols--current-symbol-bounds (list start end)))
> +             (funcall prettify-symbols-compose-predicate start end match))
>          ;; That's a symbol alright, so add the composition.
> -        (compose-region start end (cdr (assoc match alist)))
> +        (progn
> +          (compose-region start end (cdr (assoc match alist)))
> +          (add-text-properties
> +           start end
> +           `(prettify-symbols-start ,start prettify-symbols-end ,end)))
>        ;; No composition for you.  Let's actually remove any
>        ;; composition we may have added earlier and which is now
>        ;; incorrect.
> -      (remove-text-properties start end '(composition))))
> +      (remove-text-properties start end '(composition
> +                                          prettify-symbols-start
> +                                          prettify-symbols-end))))
>    ;; Return nil because we're not adding any face property.
>    nil)
>
> @@ -179,6 +187,29 @@ prettify-symbols--make-keywords
>
>  (defvar-local prettify-symbols--keywords nil)
>
> +(defvar-local prettify-symbols--current-symbol-bounds nil)
> +
> +(defun prettify-symbols--post-command-hook ()
> +  (if-let ((c (get-text-property (point) 'composition))
> +           (s (get-text-property (point) 'prettify-symbols-start))
> +           (e (get-text-property (point) 'prettify-symbols-end)))
> +      (progn
> +        (setq prettify-symbols--current-symbol-bounds (list s e))
> +        (remove-text-properties s e '(composition)))
> +    (when (and prettify-symbols--current-symbol-bounds
> +               (or (< (point) (car prettify-symbols--current-symbol-bounds))
> +                   (>= (point) (cadr prettify-symbols--current-symbol-bounds))))
> +      (apply #'font-lock-flush prettify-symbols--current-symbol-bounds)
> +      (setq prettify-symbols--current-symbol-bounds nil))))
> +
> +(defcustom prettify-symbols-unprettify-at-point t
> +  "If non-nil, show the non-prettified version of a symbol when point is on it.
> +The prettification will be reapplied as soon as point moves away
> +from the symbol.  If set to nil, the prettification persists even
> +when point is on the symbol."
> +  :type 'boolean
> +  :group 'prog-mode)
> +
>  ;;;###autoload
>  (define-minor-mode prettify-symbols-mode
>    "Toggle Prettify Symbols mode.
> @@ -206,8 +237,12 @@ prettify-symbols-mode
>          (font-lock-add-keywords nil prettify-symbols--keywords)
>          (setq-local font-lock-extra-managed-props
>                      (cons 'composition font-lock-extra-managed-props))
> +        (when prettify-symbols-unprettify-at-point
> +          (add-hook 'post-command-hook
> +                    #'prettify-symbols--post-command-hook nil t))
>          (font-lock-flush))
>      ;; Turn off
> +    (remove-hook 'post-command-hook #'prettify-symbols--post-command-hook t)
>      (when prettify-symbols--keywords
>        (font-lock-remove-keywords nil prettify-symbols--keywords)
>        (setq prettify-symbols--keywords nil))
> --8<---------------cut here---------------end--------------->8---
>
> Bye,
> Tassilo