emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Emacs-diffs] master 188f657: Fix false negatives in tex--prettify-s


From: Tassilo Horn
Subject: Re: [Emacs-diffs] master 188f657: Fix false negatives in tex--prettify-symbols-compose-p.
Date: Tue, 29 Sep 2015 21:46:49 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

Tassilo Horn <address@hidden> 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 <address@hidden>
AuthorDate: Tue Sep 29 21:34:18 2015 +0200
Commit:     Tassilo Horn <address@hidden>
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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]