From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel Subject: Re: using glyphs by default in perl-mode Date: Sun, 02 Jun 2013 08:35:47 -0400 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <87ehck6670.fsf@lifelogs.com> References: <83li7ocdkb.fsf@gnu.org> <87d2sp92vb.fsf@lifelogs.com> <87bo877kf7.fsf@lifelogs.com> <87obbs7cew.fsf@lifelogs.com> <87ip1y54fx.fsf@lifelogs.com> Reply-To: emacs-devel@gnu.org NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1370176566 19475 80.91.229.3 (2 Jun 2013 12:36:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 2 Jun 2013 12:36:06 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Jun 02 14:36:05 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Uj7W1-0001jr-AE for ged-emacs-devel@m.gmane.org; Sun, 02 Jun 2013 14:36:05 +0200 Original-Received: from localhost ([::1]:59331 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uj7W0-0005XQ-Qz for ged-emacs-devel@m.gmane.org; Sun, 02 Jun 2013 08:36:04 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54647) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uj7Vx-0005XK-14 for emacs-devel@gnu.org; Sun, 02 Jun 2013 08:36:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uj7Vv-00061k-7X for emacs-devel@gnu.org; Sun, 02 Jun 2013 08:36:00 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:34312) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uj7Vu-00061Y-TK for emacs-devel@gnu.org; Sun, 02 Jun 2013 08:35:59 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Uj7Vt-0001gf-Ee for emacs-devel@gnu.org; Sun, 02 Jun 2013 14:35:57 +0200 Original-Received: from pool-72-93-26-80.bstnma.east.verizon.net ([72.93.26.80]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 02 Jun 2013 14:35:57 +0200 Original-Received: from tzz by pool-72-93-26-80.bstnma.east.verizon.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 02 Jun 2013 14:35:57 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: emacs-devel@gnu.org Original-Lines: 253 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: pool-72-93-26-80.bstnma.east.verizon.net X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" Mail-Copies-To: never User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.3.50 (gnu/linux) Cancel-Lock: sha1:NRj6XV3YERKOo1Qmko/GNtcRS7c= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:159980 Archived-At: --=-=-= Content-Type: text/plain On Sat, 01 Jun 2013 10:47:04 -0400 Stefan Monnier 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 --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=prog-prettify2.patch Content-Transfer-Encoding: 8bit === 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 and ;; Jim Campbell . -(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)) --=-=-=--