From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Generalizing prettify-symbols-mode (was: port x-symbol to GNU emacs 24.) Date: Thu, 20 Aug 2015 10:11:44 +0200 Message-ID: <877foqidkv.fsf_-_@gnu.org> References: <87oaia6ict.fsf@mat.ucm.es> <837foxor1t.fsf@gnu.org> <878u9d7vdg.fsf@mat.ucm.es> <87pp2pq2ht.fsf@gnu.org> <87wpwx68cn.fsf@mat.ucm.es> <87mvxqxtif.fsf@gnu.org> <838u9alzly.fsf@gnu.org> <874mjyaqm8.fsf@fencepost.gnu.org> <87d1yl2dq9.fsf@gnu.org> <87r3mz91h2.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1440099429 17164 80.91.229.3 (20 Aug 2015 19:37:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 20 Aug 2015 19:37:09 +0000 (UTC) Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Aug 20 21:36:59 2015 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 1ZSVdw-0004Nq-13 for ged-emacs-devel@m.gmane.org; Thu, 20 Aug 2015 21:36:56 +0200 Original-Received: from localhost ([::1]:47110 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSKxb-0005rK-6I for ged-emacs-devel@m.gmane.org; Thu, 20 Aug 2015 04:12:31 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58431) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSKx0-0005nV-JX for emacs-devel@gnu.org; Thu, 20 Aug 2015 04:11:55 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZSKww-000257-5h for emacs-devel@gnu.org; Thu, 20 Aug 2015 04:11:54 -0400 Original-Received: from out4-smtp.messagingengine.com ([66.111.4.28]:38799) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSKwv-00024z-TI for emacs-devel@gnu.org; Thu, 20 Aug 2015 04:11:50 -0400 Original-Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1CD21202E6 for ; Thu, 20 Aug 2015 04:11:48 -0400 (EDT) Original-Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Thu, 20 Aug 2015 04:11:48 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=WXODJr+89f+aQLgrYpHUwbXiNuY=; b=aSet6 Sho8o73LprzQLhABAgdxb631FMiZIw6n7mTCx9M1xwn+kfzyr4FUKLyvsnmaUTon H25iZvEnhBbpByM1okn95T4T1i/J5tok7ga7b8nwaMirrupPTeKIWQu9xp40ka8L VKzM79t/uPYJlAbtRR+0nBkOYQ7ZTA3kbtBMUQ= X-Sasl-enc: KXmSSLXbAvX19TLFCmZVrGjELrQTnW6V3p7L1Zh41jGA 1440058307 Original-Received: from thinkpad-t440p (unknown [2.161.214.166]) by mail.messagingengine.com (Postfix) with ESMTPA id 06F5E6801C4; Thu, 20 Aug 2015 04:11:46 -0400 (EDT) Mail-Followup-To: Stefan Monnier , emacs-devel@gnu.org In-Reply-To: (Stefan Monnier's message of "Wed, 19 Aug 2015 13:29:46 -0400") User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.28 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:188977 Archived-At: Stefan Monnier 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 AuthorDate: Thu Aug 20 09:58:28 2015 +0200 Commit: Tassilo Horn 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