]> git.eshelyaron.com Git - emacs.git/commitdiff
Generalize prettify-symbols to arbitrary modes
authorTassilo Horn <tsdh@gnu.org>
Thu, 20 Aug 2015 07:58:28 +0000 (09:58 +0200)
committerTassilo Horn <tsdh@gnu.org>
Fri, 21 Aug 2015 06:41:29 +0000 (08:41 +0200)
* lisp/progmodes/prog-mode.el
(prettify-symbols-default-compose-p): New function.
(prettify-symbols-compose-predicate): New variable.
(prettify-symbols--compose-symbol): Use it.

lisp/progmodes/prog-mode.el

index 994eaaf926f1ea32224e1e64c0cb47e5ad4262a9..b8cd9d939128455636e27fe159005fef0ec8fc1e 100644 (file)
@@ -133,26 +133,41 @@ 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-default-compose-p (start end _match)
+  "Return true iff the symbol MATCH should be composed.
+The symbol starts at position START and ends at position END.
+This is default `prettify-symbols-compose-predicate' which is
+suitable for most programming languages such as C or Lisp."
+  ;; Check that the chars should really be composed into a symbol.
+  (let* ((syntaxes-beg (if (memq (char-syntax (char-after start)) '(?w ?_))
+                           '(?w ?_) '(?. ?\\)))
+         (syntaxes-end (if (memq (char-syntax (char-before end)) '(?w ?_))
+                           '(?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 matched symbol is the car of one entry in `prettify-symbols-alist'.
+The predicate receives the match's start and end position as well
+as the match-string as arguments.")
+
 (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))
-        (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)))))
+  (let ((start (match-beginning 0))
+        (end (match-end 0))
+        (match (match-string 0)))
+    (if (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)))
+      ;; 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)