all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tsdh@gnu.org>
To: Artur Malabarba <bruce.connor.am@gmail.com>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: [Emacs-diffs] master 188f657: Fix false negatives in tex--prettify-symbols-compose-p.
Date: Wed, 30 Sep 2015 08:46:47 +0200	[thread overview]
Message-ID: <87twqcwgiw.fsf@gnu.org> (raw)
In-Reply-To: <CAAdUY-LeKaSkGnOdGQ2Qye_RoXB72=jMQHRmDaPT3KFnPHUwHQ@mail.gmail.com> (Artur Malabarba's message of "Tue, 29 Sep 2015 14:14:53 +0100")

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



  reply	other threads:[~2015-09-30  6:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20150925210512.18505.12538@vcs.savannah.gnu.org>
     [not found] ` <E1ZfaB6-0004p6-FL@vcs.savannah.gnu.org>
2015-09-26  4:54   ` [Emacs-diffs] master 188f657: Fix false negatives in tex--prettify-symbols-compose-p Stefan Monnier
2015-09-26  5:36     ` Tassilo Horn
2015-09-26  5:46       ` Tassilo Horn
2015-09-29 11:29   ` Artur Malabarba
2015-09-29 12:31     ` Tassilo Horn
2015-09-29 13:14       ` Artur Malabarba
2015-09-30  6:46         ` Tassilo Horn [this message]
2015-09-30  7:48           ` Artur Malabarba
     [not found]         ` <B393F5AD12955C48A84FFB08032CD04F6C218AED@ECS-EXG-P-MB01.win.lanl.gov>
2015-09-30  7:51           ` Artur Malabarba
2015-09-30  8:59             ` Tassilo Horn
2015-09-30 10:08               ` Artur Malabarba
2015-09-30 10:45                 ` Tassilo Horn
2015-09-30 11:12                   ` Artur Malabarba
2015-09-30 11:29                     ` Tassilo Horn
2015-10-01  1:17                       ` Artur Malabarba
2015-10-01 11:43                         ` Tassilo Horn
2015-10-01  1:19                   ` Artur Malabarba
2015-10-01 11:45                     ` Tassilo Horn
2015-09-29 19:46       ` Tassilo Horn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87twqcwgiw.fsf@gnu.org \
    --to=tsdh@gnu.org \
    --cc=bruce.connor.am@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.