From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id ANn7IBwp7mQTIQEAauVa8A:P1 (envelope-from ) for ; Tue, 29 Aug 2023 19:21:32 +0200 Received: from aspmx1.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id ANn7IBwp7mQTIQEAauVa8A (envelope-from ) for ; Tue, 29 Aug 2023 19:21:32 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 2AEB859BD6 for ; Tue, 29 Aug 2023 19:21:31 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=I5Doqn2O; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1693329692; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=8d5lYs4qwthMa2NWbs8gUcLpAszKu3VOM3yGIrYPd9M=; b=Xkv7GCdPUc2k6MFD15+zwMYk/zaG5KEn/bibnGHyKuYntMZk4oX6ONCTiwXfmgr5j8vME5 +5qgmCbkkhStdQMMNJa2myM/k58ncIJ6SH01HhHqo2aKYh6X7PcoUqSymHV883f9S+eCGp hguQcYelnlZ4WtIITyRxxWCObmHabPckBD2ZzlQd4FGRm3skORtj798xZZ0RwKOoV2FQB9 LihsEWG+y1Aaje9Va4i+j9eZSmTiYcc8myIpLAVfiiktsfKuxNlxsgrPlgf6F8iNSmq2EN wMD5LWHVIft6PqC5TC9syBVxFXi9KI+v/stLIfwC9bWY6Nv77VJf76fX5nOeKg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1693329692; a=rsa-sha256; cv=none; b=arqgQtEXKDjDIORxz3KxeFO5tbdivO0IRJ40ThwoBJPJg9s7A98TYvJhyGf9dLG02njA6Q UAyxRAiW3bUcLCDta7XtwVmJMPHXkzFVEAMZGuvIg28omkPaI2Z7sDi7VN9ZKrBgBaRFrw 6yyPv1V7bQi9i6YYRJ39ONjBVUb01NciCMwybGZ5TbZeVTzcfrgYnDKwAQ9zcT8ljxo57e u5fAKLLnv1zFcqkLKPGd2g12yY+gmLvCDxZAY8z4SnqzOXl+9SrMVQGPj0BZ6hQjJj1TbE on7NUSFINoXRoZmJdQ1DUaCEXiH6sultSsOVj7TkHkba9JmvFRGSNoyLpXXLpA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=I5Doqn2O; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qb2OC-0005gC-7O; Tue, 29 Aug 2023 13:20:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qb2O3-0005dl-T0 for emacs-orgmode@gnu.org; Tue, 29 Aug 2023 13:20:20 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qb2Nz-00008R-BG for emacs-orgmode@gnu.org; Tue, 29 Aug 2023 13:20:18 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1c1f8aaab9aso11856515ad.1 for ; Tue, 29 Aug 2023 10:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693329574; x=1693934374; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8d5lYs4qwthMa2NWbs8gUcLpAszKu3VOM3yGIrYPd9M=; b=I5Doqn2OtViQeyXn4b4vnTdya7zDuE5D9Nl4JlD48FnzXrqjI29AVsVsjsUSn0D5jB mHjE6Imu37m8GGXU151cGS6iZvQaW0N6f/Os5tI3crhR1QtwT8YA6OigS8m2AP+GjCEa E4tjlvaSODBUjsW+8tv5yv5T81cZmbu0aiVUhZ5oNBv27vn9tQPAT7DMO23q0oKZfVwR Kwu18/knOIJGB3YDv3N8UJR28Xs3NqZ/UsmImskF0p2Zf9z2ci8emv4mTOShQQDcNAAU f+dIZP3u9BoGJvcQa1HMQwjYuOvDFui0YNKy09JYIwqbGpXyiAlV5a41qelE9wm5ZkzQ CmPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693329574; x=1693934374; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8d5lYs4qwthMa2NWbs8gUcLpAszKu3VOM3yGIrYPd9M=; b=alD9hBrsRAnnuXvHpST6Vp51xYE4LufmwmazK7qS8kNdS1tTD37tLBLwR1R+l3nF7r 3WQhp3Kq8swP3zK8WHCAPxnToXDg913wWGYJCHEk9RbxxqEnctRiKgkf3J7d/n96Ay8w RM1jxFbEGavnmSdWpZ76Wc3+FmnsSiSPhDPey5fUSf1WT49yl8GK1MdtuRlu07/0OZh+ rnF2FpSxJVqN9t77k9HSLa0q+gT5TIKn3Q+vv6SWuo/WQCMJcwj0cEE1cxo574pjNKbk lu3Ao4jUGVUkkyGSMxUzW17rXIZro/4x7AZ34LiKfhvVJYCoHmMpCa8Cg9CaBGJ7NoFQ 9Vow== X-Gm-Message-State: AOJu0YxuDP2zL7uO/WHcURBXaTjFkDVwERsifRncwAKWGlkRBh7TDfBd NBgFE0mgq3YEJTHwQJz4hjJovMaWG2A= X-Google-Smtp-Source: AGHT+IGu34qfX8t8PYGUsRKqdsPqMSGrz8QVI/4CcFKiXYmDMLFnLapwmX+nJA5DIA2NBP9UBniIxg== X-Received: by 2002:a17:902:6b4b:b0:1bd:a22a:d406 with SMTP id g11-20020a1709026b4b00b001bda22ad406mr25401781plt.21.1693329574309; Tue, 29 Aug 2023 10:19:34 -0700 (PDT) Received: from localhost (S01069050ca65e863.ed.shawcable.net. [174.3.244.184]) by smtp.gmail.com with ESMTPSA id 19-20020a170902c11300b001bf20c80684sm9653071pli.6.2023.08.29.10.19.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Aug 2023 10:19:33 -0700 (PDT) From: Roshan Shariff To: emacs-orgmode@gnu.org Cc: Roshan Shariff Subject: [PATCH v2] Fix background color of latex previews Date: Tue, 29 Aug 2023 11:14:22 -0600 Message-ID: <20230829171523.56558-2-roshan.shariff@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230829060145.45738-3-roshan.shariff@gmail.com> References: <20230829060145.45738-3-roshan.shariff@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=roshan.shariff@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -6.09 X-Spam-Score: -6.09 X-Migadu-Queue-Id: 2AEB859BD6 X-Migadu-Scanner: mx2.migadu.com X-TUID: c1JmJd4R98A6 This change addresses two issues: 1. Latex previews in headings have the background color of the fontified Latex code, rather than the rest of the heading. 2. If Latex code is fontified with a face that has the :extend attribute, and the preview overlay wraps to the next line, then the empty space after the end of the line uses the background color of the Latex code rather than that of the surrounding text. For example, setting org-highlight-latex-and-related to '(native) applies the org-block face to Latex code. Since the default heading faces do not set :background, the org-block background is used for preview overlays in headings. And since org-block has :extend t, its background color is also used for the empty space after the end of the line preceding a wrapped latex preview. With this change, we create a new anonymous face that inherits from the default face and sets the :extend attribute, and use it as a fallback for any attributes unspecified in the faces around the Latex code. 1. Since the fallback face inherits from default, it is guaranteed to be completely specified. The preview overlay thus appears exactly like the text around it; any unspecified attributes are taken from the default face rather than the fontified Latex code. 2. When Emacs displays the empty space at the end of a line, it only considers faces with a non-nil :extend attribute. Since the fallback face sets :extend, so will the merged preview overlay face. Thus the background color of the empty space will be taken from the surrounding text face if it sets :extend, or the default face otherwise. --- Please ignore the previous patch and look at this one instead :) * I've fixed a bug in the org-latex-preview--face-with-fallback function. * I also noticed that the patch fixes a broader issue with background colors for previews in headings. I've updated the commit message to describe both issues. lisp/org-latex-preview.el | 51 +++++++++++++++++++++++++++++++++------ lisp/org.el | 5 ++-- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index 03c198e32..96ffe894e 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -505,8 +505,7 @@ overlay face is set to `org-latex-preview-processing-face'." (overlay-put ov 'hidden-face (or (and errors 'error) - (org-latex-preview--face-around - (overlay-start ov) (overlay-end ov))))) + (org-latex-preview--overlay-face ov)))) (errors (overlay-put ov 'before-string @@ -526,8 +525,7 @@ overlay face is set to `org-latex-preview-processing-face'." (defun org-latex-preview--face-around (start end) "Return the relevant face symbol around the region START to END. A relevant face symbol before START is prefered, with END -examined if none could be found, and finally the default face -used as the final fallback. +examined if none could be found. Faces in `org-latex-preview--ignored-faces' are ignored." (or (and (> start (point-min)) (not (eq (char-before start) ?\n)) @@ -544,8 +542,47 @@ Faces in `org-latex-preview--ignored-faces' are ignored." ((consp face) (cl-set-difference face org-latex-preview--ignored-faces)) ((not (memq face org-latex-preview--ignored-faces)) - face)))) - 'default)) + face)))))) + +(defun org-latex-preview--face-with-fallback (face fallback) + "Return a face spec combining FACE with FALLBACK. +FACE should be valid as a value of the `face' text or overlay +property, as specified in Info node `(elisp)Special properties'. +The return value will also be valid as a `face' text or overlay +property. FALLBACK should be a either a face name (symbol or +string) or an anonymous face (plist); see the Info +node `(elisp)Faces'." + (pcase face + ('nil + fallback) + (`(:filtered ,filter ,face) + `(:filtered ,filter ,(org-latex-preview--face-with-fallback face fallback))) + (`(foreground-color . ,color) + `((:foreground ,color) ,fallback)) + (`(background-color . ,color) + `((:background ,color) ,fallback)) + (`(,(pred (not keywordp)) . ,_) + (append face (list fallback))) + (_ + (list face fallback)))) + +(defun org-latex-preview--overlay-face (ov) + "Return `face' property to be used for preview overlay OV. +Return the face found by `org-latex-preview--face-around', with +unset properties falling back to an anonymous face that inherits +from `default' and sets the `:extend' attribute. + +The fallback ensures that if the overlay wraps to the next line +and the text around the overlay has a face without `:extend', the +empty space after the end of the line uses the default background +color. Otherwise, the space would take on the background color +of the latex code itself if it has a face with `:extend'. For +example, when `org-highlight-latex-and-related' includes +`native', latex code is fontified with the `org-block' face." + (org-latex-preview--face-with-fallback + (org-latex-preview--face-around + (overlay-start ov) (overlay-end ov)) + '(:inherit default :extend t))) ;; Code for `org-latex-preview-auto-mode': ;; @@ -1479,7 +1516,7 @@ is either the substring between BEG and END or (when provided) VALUE." (defun org-latex-preview--colors-around (start end) "Find colors for LaTeX previews occuping the region START to END." - (let* ((face (org-latex-preview--face-around start end)) + (let* ((face (or (org-latex-preview--face-around start end) 'default)) (fg (pcase (plist-get org-latex-preview-options :foreground) ('auto (org-latex-preview--resolved-faces-attr face :foreground)) diff --git a/lisp/org.el b/lisp/org.el index 6cc8f1a07..b76f551e8 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -5439,7 +5439,7 @@ Result depends on variable `org-highlight-latex-and-related'." "\\|")))) (defvar org-latex-preview-options) ; Defined in org-latex-preview.el. -(declare-function org-latex-preview--face-around "org-latex-preview" (start end)) +(declare-function org-latex-preview--overlay-face "org-latex-preview" (ov)) (defun org-do-latex-and-related (limit) "Highlight LaTeX snippets and environments, entities and sub/superscript. @@ -5490,8 +5490,7 @@ highlighting was done, nil otherwise." (overlay-get ov 'face) (not (eq (overlay-get ov 'face) 'error))) (overlay-put ov 'face - (org-latex-preview--face-around - (overlay-start ov) (overlay-end ov)))))) + (org-latex-preview--overlay-face ov))))) (throw 'found t))))) nil)))) -- 2.41.0