From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: Using face names as FACESPEC in font-lock-keywords does not work Date: Thu, 23 Jun 2022 08:36:21 +0300 Message-ID: <83czf0eyay.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18143"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Jun 23 07:37:36 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o4FX6-0004Z4-5l for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 23 Jun 2022 07:37:36 +0200 Original-Received: from localhost ([::1]:44072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o4FX4-00037V-Of for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 23 Jun 2022 01:37:34 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33516) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o4FW4-00035s-0A for help-gnu-emacs@gnu.org; Thu, 23 Jun 2022 01:36:32 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:34806) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o4FW3-0003V1-MH for help-gnu-emacs@gnu.org; Thu, 23 Jun 2022 01:36:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=cALcp8r2oCd3P5BJpsdsphDaWZ90zPdFfyZ55K3iGFc=; b=GJsX4Jb+fa1rkwingzSW B2p79j2DAgeUdw29+nKeycdwFuZ+zO33XLcLWtJGK8PGY9HN0X9KccSPKjTugQDtAMi+R77SyNga1 DOVL3EcmYLttex7stHl5TW0tiyeA8pvZc+fvQY6ihOnhkBoopxVIZwUturez9EilECGQMD7ihgd3S Su2dE9eBiDfl9C1ByvGXMUViTgd6YOERIpfloG/BwnF/T2LxlqfCVk0vfwL1faky9aMXjJiF8M1+8 kreKFlWQkDcEzUIUcYtVl7rtOJnC/WCV1mjSOiQtbcGD8uxCPNkPd2klZ+qDaQpKyWL4tjNI4nbkS 3j8tQKPVkRXakA==; Original-Received: from [87.69.77.57] (port=4073 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o4FW3-0002kB-52 for help-gnu-emacs@gnu.org; Thu, 23 Jun 2022 01:36:31 -0400 In-Reply-To: (jan.synacek@posteo.org) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:137999 Archived-At: > Date: Wed, 22 Jun 2022 08:33:49 +0000 > From: jan.synacek@posteo.org > > I have the following piece of code that is supposed to add highlighting > for CPP-like conditionals in my mode: > > (setq-local font-lock-defaults > (let* ((cpp-rx (rx line-start > "#" (or "if" "ifdef" "else" "elif" > "endif") > word-boundary)) > (keywords > `((,cpp-rx . xref-file-header)))) > (list keywords))) > > This piece of code does not work and does not use the 'xref-file-header' > face. > > > In the Elisp manual (24.6.2 Search-based Fontification) it says: > " > -- Variable: font-lock-keywords > ... > > ... > Each element of ‘font-lock-keywords’ should have one of these forms: > ... > ‘(MATCHER . FACESPEC)’ > In this kind of element, FACESPEC is an expression whose value > specifies the face to use for highlighting. In the simplest case, > FACESPEC is a Lisp variable (a symbol) whose value is a face name. > ... > " > > I find the language here quite confusing. 'xref-file-header' is a symbol > that evaluates to itself, which also happens to be the face name, and is > technically an expression. But apparently, that's not enough, as it > seems that the symbol itself has to have a value cell in this particular > case. The documentation you cite is of font-lock-keywords, but your code uses font-lock-defaults, a variable whose value is supposed to be something very different. If that is a typo, then which of these two is correct? > Also, in 40.12.2 Defining Faces, it says: > " > People are sometimes tempted to create a variable whose value is a > face name. In the vast majority of cases, this is not necessary; the > usual procedure is to define a face with ‘defface’, and then use its > name directly. > " > > This piece of documentation doesn't really help, too. Please note that it describes a different use case: how to _define_ a face, not how to use faces in font-lock. Faces can be used in Emacs in contexts completely unrelated to font-lock. > So my question is: Am I doing something wrong? Or does this case > (setting font-lock-keywords) really > require a variable which holds the face name instead of using the face > name directly? My suggestion is to use one of the font-lock faces, not something unrelated. I don't really understand how xref-file-header face could be related to highlighting CPP-like conditionals in the first place. If you like its colors, why not customize one of the rarely-used font-lock faces to have those colors, and then use that? But if you insist on using the above, and if you must understand how to force Emacs to do what you want in that case, that's possibly a separate discussion, so please do indicate your intentions. (And yes, faces are a tricky subject and can easily confuse in corner cases, and your code uses some "advanced" techniques, so a little wonder you bump into that.)