emacs-devel
[Top][All Lists]
Advanced

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

Re: using glyphs by default in perl-mode


From: Ted Zlatanov
Subject: Re: using glyphs by default in perl-mode
Date: Sat, 01 Jun 2013 09:46:42 -0400
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.3.50 (gnu/linux)

On Sat, 01 Jun 2013 02:29:05 -0400 Dan Nicolaescu <address@hidden> wrote: 

DN> Stefan Monnier <address@hidden> writes:
DN> Can you please disable this by default until it's done?  It has negative
DN> and annoying and very hard to remove effects...
>>> Maintainers: OK to do this?  I'll take care of the details.
>> 
>> I'd rather we do it all in one go.  It can't be that hard to add
>> a config var to prog-mode and then use it in perl-mode.

DN> The final result still is that the feature is off by default, right?
DN> emacs cannot be used to edit perl in some configurations, so the faster
DN> this ends the better.

OK, so this only happens if font-lock decoration level more than 2 is
requested: in perl-mode.el, we do

#+begin_src lisp
  (setq font-lock-defaults '((perl-font-lock-keywords
                              perl-font-lock-keywords-1
                              perl-font-lock-keywords-2)
...
#+end_src

The keywords-2 list is the one with the Unicode glyphs.

So perhaps there should be a way to specify max font-lock decoration in
terminal vs. graphical.  Anyhow, just something I noticed... no big deal.

The attached patch does the following:

1) move `perl--prettify-symbols-alist' and the functions for it,
`perl--font-lock-compose-symbol' and `perl--font-lock-symbols-keywords'
to simple.el (with the "prog-" prefix).

2) declare `perl-prettify-symbols' obsolete in favor of the alist
`prog-prettify-symbols'.

I gave `prog-prettify-symbols' two default choices:
`prog--prettify-symbols-alist-basic' (just -> => ::) and
`prog--prettify-symbols-alist-extended' (basic plus much more).  It can
also be a free-form alist with key=string, value=character.  I expect
programming modes will need to augment this list somehow, but I'd like
the user to simply say "I want basics or extended" instead of having to
select individual glyphs for every language.  Maybe the choice should be
between :basic and :extended as symbols?  Ideas welcome.

`prog-prettify-symbols' defaults to nil, so this feature is turned off
by default.

3) tell `perl-mode' to use `prog--font-lock-symbols-keywords' instead of
`perl--font-lock-symbols-keywords'.

The patch grows simple.el.  Perhaps it should live in prog.el or
prettify.el.  prog.el seems more sensible.

I can't get `perl-mode' to show the symbols, so the patch is broken
right now (I checked the data structures and they're OK; this is
something in the font-lock plumbing I think).  I also wonder if it's
possible to add the prettify support at the `prog-mode' level, so each
mode doesn't have to explicitly inline those keywords.

Unfortunately my font-lock-fu is not good, so I'd really appreciate help
with the last two issues.  I spent a few hours on them but couldn't work
them out.

Thanks
Ted

=== modified file 'lisp/progmodes/perl-mode.el'
--- lisp/progmodes/perl-mode.el 2013-05-07 18:06:13 +0000
+++ lisp/progmodes/perl-mode.el 2013-06-01 13:28:52 +0000
@@ -163,39 +163,7 @@
   :version "24.4"
   :type 'boolean)
 
-(defconst perl--prettify-symbols-alist
-  '(;;("andalso" . ?∧) ("orelse"  . ?∨) ("as" . ?≡)("not" . ?¬)
-    ;;("div" . ?÷) ("*"   . ?×) ("o"   . ?○)
-    ("->"  . ?→)
-    ("=>"  . ?⇒)
-    ;;("<-"  . ?←) ("<>"  . ?≠) (">="  . ?≥) ("<="  . ?≤) ("..." . ?⋯)
-    ("::" . ?∷)
-    ))
-
-(defun perl--font-lock-compose-symbol ()
-  "Compose a sequence of ascii chars 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 (if (eq (char-syntax (char-after start)) ?w)
-                      '(?w) '(?. ?\\))))
-    (if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes)
-           (memq (char-syntax (or (char-after end) ?\ )) syntaxes)
-            (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-string 0)
-                                            perl--prettify-symbols-alist)))))
-  ;; Return nil because we're not adding any face property.
-  nil)
-
-(defun perl--font-lock-symbols-keywords ()
-  (when perl-prettify-symbols
-    `((,(regexp-opt (mapcar 'car perl--prettify-symbols-alist) t)
-       (0 (perl--font-lock-compose-symbol))))))
+(make-obsolete-variable 'perl-prettify-symbols 'prog-prettify-symbols "24.4")
 
 (defconst perl-font-lock-keywords-1
   '(;; What is this for?
@@ -244,7 +212,7 @@
      ("\\<\\(continue\\|goto\\|last\\|next\\|redo\\)\\>[ \t]*\\(\\sw+\\)?"
       (1 font-lock-keyword-face) (2 font-lock-constant-face nil t))
      ("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)
-     ,@(perl--font-lock-symbols-keywords)))
+     ,@(prog--font-lock-symbols-keywords)))
   "Gaudy level highlighting for Perl mode.")
 
 (defvar perl-font-lock-keywords perl-font-lock-keywords-1

=== modified file 'lisp/simple.el'
--- lisp/simple.el      2013-05-25 02:21:49 +0000
+++ lisp/simple.el      2013-06-01 13:23:00 +0000
@@ -400,6 +400,55 @@
   ;; Any programming language is always written left to right.
   (setq bidi-paragraph-direction 'left-to-right))
 
+(defconst prog--prettify-symbols-alist-basic
+  '(("->"  . ?→)
+    ("=>"  . ?⇒)
+    ("::" . ?∷)
+    )
+  "Basic list of symbols to prettify, useful in most languages.")
+
+(defconst prog--prettify-symbols-alist-extended
+  (append prog--prettify-symbols-alist-basic
+          '(("andalso" . ?∧) ("orelse"  . ?∨) ("as" . ?≡)("not" . ?¬)
+            ("div" . ?÷) ("*"   . ?×) ("o"   . ?○)
+            ("<-"  . ?←) ("<>"  . ?≠) (">="  . ?≥) ("<="  . ?≤) ("..." . ?⋯)
+            ))
+  "Extended list of symbols to prettify.")
+
+(defcustom prog-prettify-symbols nil
+  "Which symbols should be prettified.
+Set to an alist in the form `((\"->\" . ?→) ...)'."
+  :type `(choice
+          (const :tag "Basic list: -> => ::" 
,prog--prettify-symbols-alist-basic)
+          (const :tag "Extended list: basic plus much more" 
,prog--prettify-symbols-alist-extended)
+          (alist :tag "Define your own list" :key-type string :value-type 
character))
+  :group 'languages)
+
+(defun prog--font-lock-compose-symbol ()
+  "Compose a sequence of ascii chars 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 (if (eq (char-syntax (char-after start)) ?w)
+                      '(?w) '(?. ?\\))))
+    (if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes)
+           (memq (char-syntax (or (char-after end) ?\ )) syntaxes)
+            (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-string 0)
+                                            prog--prettify-symbols-alist)))))
+  ;; Return nil because we're not adding any face property.
+  nil)
+
+(defun prog--font-lock-symbols-keywords ()
+  (when prog-prettify-symbols
+    `((,(regexp-opt (mapcar 'car prog-prettify-symbols) t)
+       (0 (prog--font-lock-compose-symbol))))))
+
 ;; Making and deleting lines.
 
 (defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard))


reply via email to

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