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: Tue, 29 Sep 2015 21:46:49 +0200	[thread overview]
Message-ID: <87y4fphuty.fsf@gnu.org> (raw)
In-Reply-To: <8737xxjtkq.fsf@gnu.org> (Tassilo Horn's message of "Tue, 29 Sep 2015 14:31:01 +0200")

Tassilo Horn <tsdh@gnu.org> writes:

Hi Artur,

>> Would it be possible to also use `tex--prettify-symbols-compose-p' to
>> avoid composing the symbol-at-point? (perhaps by checking
>> window-point or something). That would toggleable by a user-option,
>> of course.
>
> You mean, when point enters a prettified symbol the original text
> would be shown?  Indeed, that sounds like a neat idea.  If I think
> about it, I don't use prettification in LaTeX exactly because there
> are so many prettified symbols which partly overlap and then editing
> becomes cumbersome, e.g., when deleting a char from the prettified
> integral \int you'll get the prettification of the set membership
> relation \in.
>
>> Or maybe that feature should be implemented in `prettify-symbols-mode'
>> itself.
>
> Yes, I think the only thing to be done would be to change the
>
>   (if (funcall prettify-symbols-compose-predicate start end match) ...
>
> in `prettify-symbols--compose-symbol' to
>
>   (if (and (or prettify-symbols-compose-at-point
>                (< (window-point) start)
>                (> (window-point) end))
>            (funcall prettify-symbols-compose-predicate start end match)))
>
> where `prettify-symbols-compose-at-point' would be the user option.

Well, this approach didn't work because during font-lock both `point'
and `window-point' equal `end', i.e., the end of the match of the search
font-lock performed.

Then I experimented with the `point-entered' and `point-left' text
properties but apparently the functions I added there were never ever
called.  On the internets I have read that these two props are much more
low-level than what one would expect and in general it would be better
to track point in a `post-command-hook'.

So that's what I did, and the following patch works fine for me.
Comments welcome!

--8<---------------cut here---------------start------------->8---
prettify-symbols-ext 7c2ef9b8948df4c05d0c0aa28d85a128f975e453
Author:     Tassilo Horn <tsdh@gnu.org>
AuthorDate: Tue Sep 29 21:34:18 2015 +0200
Commit:     Tassilo Horn <tsdh@gnu.org>
CommitDate: Tue Sep 29 21:34:18 2015 +0200

Parent:     888d644 * net/shr.el (shr-colorize-region): Don't do it on a system not supporting 256 above colors (bug#21557).
Merged:     master prettify-symbols-ext
Containing: prettify-symbols-ext
Follows:    emacs-24.5-rc3-fixed (6045)

Implement unprettification of symbol at point

* lisp/progmodes/prog-mode.el: Implement feature for unprettifying the
symbol at point.
(prettify-symbols--current-symbol-bounds): New variable.
(prettify-symbols--post-command-hook): New function.
(prettify-symbols-unprettify-at-point): New defcustom.
(prettify-symbols-mode): Use it.
(prettify-symbols--compose-symbol): Use them.

1 file changed, 36 insertions(+), 3 deletions(-)
lisp/progmodes/prog-mode.el | 39 ++++++++++++++++++++++++++++++++++++---

modified   lisp/progmodes/prog-mode.el
@@ -161,13 +161,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 +186,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 ((_ (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,6 +236,9 @@ 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
     (when prettify-symbols--keywords

--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



      parent reply	other threads:[~2015-09-29 19: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
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 [this message]

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=87y4fphuty.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.