emacs-devel
[Top][All Lists]
Advanced

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

Generalizing prettify-symbols-mode (was: port x-symbol to GNU emacs 24.)


From: Tassilo Horn
Subject: Generalizing prettify-symbols-mode (was: port x-symbol to GNU emacs 24.)
Date: Thu, 20 Aug 2015 10:11:44 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> Indeed.  And if we want to handle \alpha2 correctly, I think we'll
> need to extend prettify-symbols-mode somehow to let latex-mode teach
> prettify-symbols-mode how to properly recognize whether a given \alpha
> should be composed or not.
>
> In some earlier prettify-symbols code (the one used in haskell-mode),
> I extended the alist so that each element can have a predicate
> checking whether this occurrence should be composed (this is used in
> Haskell for the "." which can either be the function-composition infix
> operator, or the usual separator used within identifiers as in
> List.map).
>
> We should probably extend prettify-symbols-alist similarly, but in the
> case of TeX, I think setting a predicate for every entry is
> inconvenient, and we should instead set a "global" predicate which is
> applied to every entry (and is used instead of the default code which
> checks the syntax-class of surrounding characters).

Is the below what you are suggesting?  That's the "global" (aka
mode/buffer specific predicate) version.  I don't think we need to
extend `prettify-symbols-alist'.  If the composition-predicate is
different for some symbols, then the new
`prettify-symbols-compose-predicate' can do that distinction, too.

And in the (IMHO unlikely) case that a user wants to add symbols which
require a different special-casing, he can just define his own
compose-predicate handling his own symbols and delegating to the default
predicate for that mode in the other cases.

--8<---------------cut here---------------start------------->8---
References: prettify-symbols
Author:     Tassilo Horn <address@hidden>
AuthorDate: Thu Aug 20 09:58:28 2015 +0200
Commit:     Tassilo Horn <address@hidden>
CommitDate: Thu Aug 20 10:03:46 2015 +0200

    Generalize prettify-symbols to arbitrary modes
    
    * lisp/progmodes/prog-mode.el
    (prettify-symbols-default-compose-p): New function.
    (prettify-symbols-compose-predicate): New variable with default
    value #'prettify-symbols-default-compose-p.
    (prettify-symbols--compose-symbol): Use
    prettify-symbols-compose-predicate.

1 parent commit, 2 merged branches, 1 containing branch
 Parent     | d0079c9 In `widget-color--choose-action' quit *Color* window 
instead of deleting it
 Merged     | master prettify-symbols
 Containing | prettify-symbols
 Follows    | emacs-24.5-rc3-fixed (5619)

1 file changed, 28 insertions(+), 14 deletions(-)
 lisp/progmodes/prog-mode.el | 42 ++++++++++++++++++++++++++++--------------

modified   lisp/progmodes/prog-mode.el
@@ -133,26 +133,40 @@ Each element looks like (SYMBOL . CHARACTER), where the 
symbol
 matching SYMBOL (a string, not a regexp) will be shown as
 CHARACTER instead.")
 
-(defun prettify-symbols--compose-symbol (alist)
-  "Compose a sequence of characters into a symbol.
-Regexp match data 0 points to the chars."
+(defun prettify-symbols-default-compose-p ()
+  "The default `prettify-symbols-compose-predicate'.
+Suitable for most programming languages such as C or Lisp."
   ;; Check that the chars should really be composed into a symbol.
   (let* ((start (match-beginning 0))
         (end (match-end 0))
         (syntaxes-beg (if (memq (char-syntax (char-after start)) '(?w ?_))
                            '(?w ?_) '(?. ?\\)))
         (syntaxes-end (if (memq (char-syntax (char-before end)) '(?w ?_))
-                      '(?w ?_) '(?. ?\\)))
-        match)
-    (if (or (memq (char-syntax (or (char-before start) ?\s)) syntaxes-beg)
-           (memq (char-syntax (or (char-after end) ?\s)) syntaxes-end)
-            ;; syntax-ppss could modify the match data (bug#14595)
-            (progn (setq match (match-string 0)) (nth 8 (syntax-ppss))))
-       ;; 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))
-      ;; That's a symbol alright, so add the composition.
-      (compose-region start end (cdr (assoc match alist)))))
+                           '(?w ?_) '(?. ?\\))))
+    (not (or (memq (char-syntax (or (char-before start) ?\s)) syntaxes-beg)
+             (memq (char-syntax (or (char-after end) ?\s)) syntaxes-end)
+             (nth 8 (syntax-ppss))))))
+
+(defvar-local prettify-symbols-compose-predicate 
#'prettify-symbols-default-compose-p
+  "A predicate deciding if the currently matched symbol is to be composed.
+The predicate has no arguments and is called by font-lock in the
+context where the regexp match data 0 points to an occurrence of
+a symbol in `prettify-symbols-alist'.")
+
+(defun prettify-symbols--compose-symbol (alist)
+  "Compose a sequence of characters into a symbol.
+Regexp match data 0 points to the chars."
+  ;; Check that the chars should really be composed into a symbol.
+  (let ((start (match-beginning 0))
+        (end (match-end 0))
+        (match (match-string 0)))
+    (if (funcall prettify-symbols-compose-predicate)
+        ;; That's a symbol alright, so add the composition.
+        (compose-region start end (cdr (assoc match alist)))
+      ;; 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))))
   ;; Return nil because we're not adding any face property.
   nil)
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



reply via email to

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