From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "Paul W. Rankin" Newsgroups: gmane.emacs.devel Subject: font-lock-maximum-decoration API Date: Mon, 20 May 2019 13:28:29 +1000 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="247503"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: mu4e 1.2.0; emacs 26.2 To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 20 05:28:46 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSYyj-0012Gr-4G for ged-emacs-devel@m.gmane.org; Mon, 20 May 2019 05:28:45 +0200 Original-Received: from localhost ([127.0.0.1]:57199 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSYyh-0005DQ-OW for ged-emacs-devel@m.gmane.org; Sun, 19 May 2019 23:28:43 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:51886) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSYya-0005Cy-Vs for emacs-devel@gnu.org; Sun, 19 May 2019 23:28:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hSYyZ-00032s-Mq for emacs-devel@gnu.org; Sun, 19 May 2019 23:28:36 -0400 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:47741) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hSYyZ-00030r-AO for emacs-devel@gnu.org; Sun, 19 May 2019 23:28:35 -0400 Original-Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 1DA80346 for ; Sun, 19 May 2019 23:28:34 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 19 May 2019 23:28:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paulwrankin.com; h=from:to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=fm3; bh=u/7yL5Q/Cw/l4jXHi1j+Q4Q70P E4z5o+MS2T+7zP8N0=; b=OGWlQ2yDKb5pGVb5cM43xopzPj1TM4Z9ph2SHQ6Qqe Qp4lypwSrClq/tz6bNBbjBlIFk2yGDFi3wtappKaRdi3aJECqBZ+JvhcOIgqCExL 8Mq2AWQHhYlLoGxOZ2y318tjE9rE+bJKZyh/9+Pm/VQxUXQstWjAaZxcHC/l1nt0 DU8yCGJMkeKgwQWZPj5u+NrPPGo0LaFtrCds8Utv5DNUQ1mxjJv+a1oaZXq9vYFk gVTXzudCu9hjBDHblA+flInt2GTAiaoyUlkPtLcGTUtyX2mp5hHqc3sCilem88ZN 6UGf+e826DjaETm1QKl+lGQpvP1F//XNX3xWcLD3P06Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=u/7yL5 Q/Cw/l4jXHi1j+Q4Q70PE4z5o+MS2T+7zP8N0=; b=bu9iXnpUIMe2duS1kxIV8o H18XPI6I5vIlnic7zqAJ5Cxss2PFGmkVfTeDI15ZxnMyf+qa2Zpq3vnzFm3b/X+F bG6ckv4E9RnoGJGHdsCbiUlGitlzOjekPydhWmI909cHK6pft/7Q+Kvd7xQWwsYb zEO1VGfmuRvOpIier6hpVF0E6XM9HQmizezr749lZN7hfHa3l75uONpEa0Rczpwl rifkH5v+LkOaUrapgGQEa9SWR3ei8C99GRTrmadQtktfZCJZPFvVcPpZtXzjLGhZ MAzfiRjJyubPNb1HaOIFf8n2LTINf60RNOtJ0z9v42ISASeUgVq4YEnJMsA2/50Q == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddtjedgjeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegfhffvufffkfggtgfgsehtqhertd dtreejnecuhfhrohhmpedfrfgruhhlucghrdcutfgrnhhkihhnfdcuoehhvghllhhosehp rghulhifrhgrnhhkihhnrdgtohhmqeenucffohhmrghinhepphgruhhlfihrrghnkhhinh drtghomhdpghhnuhdrohhrghenucfkphepudeggedrudeftddrudehiedruddvleenucfr rghrrghmpehmrghilhhfrhhomhephhgvlhhlohesphgruhhlfihrrghnkhhinhdrtghomh enucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Original-Received: from localhost (unknown [144.130.156.129]) by mail.messagingengine.com (Postfix) with ESMTPA id 846E480059 for ; Sun, 19 May 2019 23:28:32 -0400 (EDT) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.147.123.19 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.devel:236770 Archived-At: wrt.=20 http://lists.gnu.org/archive/html/help-gnu-emacs/2019-05/msg00022.html There is a little-used Emacs feature to allow different levels of=20 font-lock decoration via the variable=20 font-lock-maximum-decoration: > You can customize the variable =E2=80=98font-lock-maximum-decoration=E2= =80=99 to=20 > alter the amount of fontification applied by Font Lock mode, for=20 > major modes that support this feature. The value should be a=20 > number (with 1 representing a minimal amount of fontification;=20 > some modes support levels as high as 3); or =E2=80=98t=E2=80=99, meaning = =E2=80=9Cas=20 > high as possible=E2=80=9D (the default).[1] I've noticed that few major modes implement multiple levels of=20 font-lock decoration, which might be because it's more difficult=20 to dynamically create different font-lock keywords, but it also=20 could be because there seems to be no API for programmatically=20 changing this value per major mode, as the variable permits: > If a list, each element should be a cons pair of the form=20 > (MAJOR-MODE . LEVEL), > where MAJOR-MODE is a symbol or t (meaning the default). For=20 > example: > ((c-mode . t) (c++-mode . 2) (t . 1)) > means use the maximum decoration available for buffers in C=20 > mode, level 2 > decoration for buffers in C++ mode, and level 1 decoration=20 > otherwise. When writing fountain-mode, I wanted several levels of font-lock,=20 and so needed to implement a couple of functions to get and set=20 this variable, but this felt a bit outside a major mode's bounds. I'd love to see more major modes offering multiple levels of=20 font-lock[3] so it would be nice if major modes could call a=20 function to safely set this variable for themselves, without=20 needing to handle the whole list. If at all helpful, I've included the functions in fountain-mode.=20 As this code is in GNU ELPA, it remains available to implement=20 into Emacs if anyone wishes: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (defun fountain-get-font-lock-decoration () "Return the value of `font-lock-maximum-decoration' for=20 `fountain-mode'." (let ((n (if (listp font-lock-maximum-decoration) (cdr (or (assq 'fountain-mode=20 font-lock-maximum-decoration) (assq 't font-lock-maximum-decoration))) font-lock-maximum-decoration))) (cond ((null n) 2) ((eq n t) 3) ((integerp n) n) (t 2)))) (defun fountain-set-font-lock-decoration (n) "Set `font-lock-maximum-decoration' for `fountain-mode' to N." (interactive "NMaximum decoration (1-3): ") (if (and (integerp n) (<=3D 1 n 3)) (let ((level (cond ((=3D n 1) 1) ((=3D n 2) nil) ((=3D n 3) t)))) (cond ((listp font-lock-maximum-decoration) (setq font-lock-maximum-decoration (assq-delete-all 'fountain-mode=20 font-lock-maximum-decoration)) (customize-set-variable=20 'font-lock-maximum-decoration (cons (cons 'fountain-mode=20 level) font-lock-maximum-decoration))) ((or (booleanp font-lock-maximum-decoration) (integerp font-lock-maximum-decoration)) (customize-set-variable=20 'font-lock-maximum-decoration (list (cons 'fountain-mode=20 level) (cons 't=20 font-lock-maximum-decoration))= ))) (message "Syntax highlighting is now: %s" (cond ((=3D n 1) "minimum") ((=3D n 2) "default") ((=3D n 3) "maximum"))) (font-lock-refresh-defaults)) (user-error "Decoration must be an integer 1-3"))) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1]: (info "(emacs) Font Lock") [2]: Something a little silly about this option is that the=20 defcustom includes this tag: (const :tag "default" nil) Which implies to the user that nil or the 2nd highest level is=20 default, despite t or 3rd highest being the Emacs default. The=20 question of which level is the "default" is pretty inconsistent=20 between the variable docstring, the manual, and the code=20 implementation itself. [3]: I'd also like to see more major modes correctly using=20 font-lock rather than their own dysfunctional mutant=20 abominations... -- https://www.paulwrankin.com