[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: |
Sun, 02 Jun 2013 08:35:47 -0400 |
User-agent: |
Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.3.50 (gnu/linux) |
On Sat, 01 Jun 2013 10:47:04 -0400 Stefan Monnier <address@hidden> wrote:
>> 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).
SM> You might like to consider the pretty-lambda for Lisp as well as the
SM> sml-font-lock-symbols in elpa/packages/sml-mode/sml-mode.el.
SM> [ There are others out there for, e.g. for Hasekll-mode. ]
OK, I'll add those once `perl-mode' works.
>> 2) declare `perl-prettify-symbols' obsolete in favor of the alist
>> `prog-prettify-symbols'.
SM> No need for such obsolescence declaration, since it's never been in any
SM> released version.
OK.
>> 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.
SM> In my experience, every major mode might want to provide its own list.
OK, see patch-2 below.
>> The patch grows simple.el. Perhaps it should live in prog.el or
>> prettify.el. prog.el seems more sensible.
SM> I think prog-mode.el is in order, yes.
I'll do that in patch-3, for now it's all in simple.el.
>> 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.
SM> That would be nice, indeed. But I fear it's going to be tricky to make
SM> it work right, so let's keep it for later.
OK. We now build the list of keywords dynamically when `perl-mode' is
called, based on `prog-prettify-symbols'. That variable can be 'basic,
'extended, 'all (a symbol does a lookup in the buffer-local
`prog-prettify-symbols-alist') or a directly defined alist.
On the `perl-mode' side, you just do
#+begin_src lisp
(setq-local prog-prettify-symbols-alist perl--prettify-symbols-alist)
#+end_src
and then `(prog-font-lock-symbols-keywords)' automatically will give you
the font-lock 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.
SM> I don't have much time to devote right now, but the current problem is
SM> probably a minor oversight.
I have the data working and AFAICT the correct data is passed to
font-lock but something is going wrong inside the fontification call.
That's hard to debug. I'll work on it more (I think you posted a trick
for debugging font-lock a while ago) but if anyone can spot the issue, I
would appreciate any suggestions.
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-02 04:30:35 +0000
@@ -158,44 +158,21 @@
;; Regexps updated with help from Tom Tromey <address@hidden> and
;; Jim Campbell <address@hidden>.
-(defcustom perl-prettify-symbols t
- "If non-nil, some symbols will be displayed using Unicode chars."
- :version "24.4"
- :type 'boolean)
+(defconst perl--prettify-symbols-alist-basic
+ '(("->" . ?→)
+ ("=>" . ?⇒)
+ ("::" . ?∷)))
+
+(defconst perl--prettify-symbols-alist-extended
+ `(,@perl--prettify-symbols-alist-basic
+ ("andalso" . ?∧) ("orelse" . ?∨) ("as" . ?≡)("not" . ?¬)
+ ("div" . ?÷) ("*" . ?×) ("o" . ?○)
+ ("<-" . ?←) ("<>" . ?≠) (">=" . ?≥) ("<=" . ?≤) ("..." . ?⋯)))
(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))))))
+ `((basic ,@perl--prettify-symbols-alist-basic)
+ (extended ,@perl--prettify-symbols-alist-extended)
+ (all ,@perl--prettify-symbols-alist-extended)))
(defconst perl-font-lock-keywords-1
'(;; What is this for?
@@ -243,13 +220,17 @@
;; Fontify keywords with/and labels as we do in `c++-font-lock-keywords'.
("\\<\\(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)))
+ ("^[ \t]*\\(\\sw+\\)[ \t]*:[^:]" 1 font-lock-constant-face)))
"Gaudy level highlighting for Perl mode.")
(defvar perl-font-lock-keywords perl-font-lock-keywords-1
"Default expressions to highlight in Perl mode.")
+;; used to add font-lock keywords dynamically
+(defvar perl-augmented-font-lock-keywords)
+(defvar perl-augmented-font-lock-keywords-1)
+(defvar perl-augmented-font-lock-keywords-2)
+
(defvar perl-quote-like-pairs
'((?\( . ?\)) (?\[ . ?\]) (?\{ . ?\}) (?\< . ?\>)))
@@ -685,11 +666,22 @@
(setq-local comment-start-skip "\\(^\\|\\s-\\);?#+ *")
(setq-local comment-indent-function #'perl-comment-indent)
(setq-local parse-sexp-ignore-comments t)
+(setq prog-prettify-symbols 'all)
+ ;; Define the symbols to be prettified
+ (setq-local prog-prettify-symbols-alist perl--prettify-symbols-alist)
+
;; Tell font-lock.el how to handle Perl.
- (setq font-lock-defaults '((perl-font-lock-keywords
- perl-font-lock-keywords-1
- perl-font-lock-keywords-2)
- nil nil ((?\_ . "w")) nil
+ (setq perl-augmented-font-lock-keywords (append perl-font-lock-keywords
+ (prog-font-lock-symbols-keywords)))
+ (setq perl-augmented-font-lock-keywords-1 (append perl-font-lock-keywords-1
+ (prog-font-lock-symbols-keywords)))
+ (setq perl-augmented-font-lock-keywords-2 (append perl-font-lock-keywords-2
+ (prog-font-lock-symbols-keywords)))
+
+ (setq font-lock-defaults '((perl-augmented-font-lock-keywords
+ perl-augmented-font-lock-keywords-1
+ perl-augmented-font-lock-keywords-2)
+ nil nil ((?\_ . "w")) nil
(font-lock-syntactic-face-function
. perl-font-lock-syntactic-face-function)))
(setq-local syntax-propertize-function #'perl-syntax-propertize-function)
=== modified file 'lisp/simple.el'
--- lisp/simple.el 2013-05-25 02:21:49 +0000
+++ lisp/simple.el 2013-06-02 04:03:20 +0000
@@ -397,9 +397,51 @@
"Major mode for editing programming language source code."
(set (make-local-variable 'require-final-newline) mode-require-final-newline)
(set (make-local-variable 'parse-sexp-ignore-comments) t)
+ (set (make-local-variable 'prog-prettify-symbols-alist) nil)
;; Any programming language is always written left to right.
(setq bidi-paragraph-direction 'left-to-right))
+(defcustom prog-prettify-symbols nil
+ "Which symbols should be prettified.
+When set to `basic' or `extended' or `all', the actual choices
+are made by the mode that derives from `prog-mode'."
+ :type '(choice
+ (const :tag "No thanks" nil)
+ (const :tag "Basic list" basic)
+ (const :tag "Extended list: basic plus much more" extended)
+ (const :tag "Everything: because you can!" all)
+ (alist :tag "Define your own list" :key-type string :value-type
character))
+ :group 'languages)
+
+(defun prog--font-lock-compose-symbol (alist)
+ "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) alist)))))
+ ;; Return nil because we're not adding any face property.
+ nil)
+
+(defun prog-font-lock-symbols-keywords ()
+ (when prog-prettify-symbols
+ (let ((alist (if (symbolp prog-prettify-symbols)
+ ;; the cdr of the cons cell has all we need
+ (cdr (assoc prog-prettify-symbols
+ prog-prettify-symbols-alist))
+ prog-prettify-symbols)))
+ `((,(regexp-opt (mapcar 'car alist) t)
+ (0 (prog--font-lock-compose-symbol ,alist)))))))
+
;; Making and deleting lines.
(defvar hard-newline (propertize "\n" 'hard t 'rear-nonsticky '(hard))
- Re: using glyphs by default in perl-mode, Dan Nicolaescu, 2013/06/01
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/01
- Re: using glyphs by default in perl-mode, Stefan Monnier, 2013/06/01
- Re: using glyphs by default in perl-mode,
Ted Zlatanov <=
- Re: using glyphs by default in perl-mode, Stefan Monnier, 2013/06/02
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/02
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/02
- Re: using glyphs by default in perl-mode, Stefan Monnier, 2013/06/03
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/03
- Re: using glyphs by default in perl-mode, Davis Herring, 2013/06/04
- Re: using glyphs by default in perl-mode, Stefan Monnier, 2013/06/04
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/04
- Re: using glyphs by default in perl-mode, Stefan Monnier, 2013/06/04
- Re: using glyphs by default in perl-mode, Ted Zlatanov, 2013/06/05