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 kPfsIcqK7WSeGAEAauVa8A:P1 (envelope-from ) for ; Tue, 29 Aug 2023 08:06:02 +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 kPfsIcqK7WSeGAEAauVa8A (envelope-from ) for ; Tue, 29 Aug 2023 08:06:02 +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 8F8BA50707 for ; Tue, 29 Aug 2023 08:06:01 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="KRD1P/CI"; 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"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1693289161; 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=Bpo34j7yd4F+1SMGH8Yhtr3TfrnMeVgX7G+7A7drwqQ=; b=GGqXhSPSUlcFxrXJUenQ9PTRYRm3mftPed8OglnFEz3hGS5mLGO1q4Ayiuwx0JQejuaHGx +KYhjRdgj54eVCp7Ntg1JfL8O1KBQCN8UP4ExB1T8ayYTUY4vBy+MUasHNWO/3OmuQou8d CT08iXNgOOXo/2j2rfhN/UxZWxsBrUbY7usgYX1KgNeMcuMYehTYqldQ7hNMHxSTMwLxCb DLl+Re6mepj2mvVDQQnkQWapuPzPk+I3qUdtP2bJ+PnnWethRDRqwHsNbYwuZtDfQys5/4 dW5ArPd6U13y46NawjoQzSb9cMkvzew+DWZ5ZJz6RptL3d9N/qwrWyBEEh8YKA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="KRD1P/CI"; 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"; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=key1; d=yhetil.org; t=1693289161; a=rsa-sha256; cv=none; b=C1GiJUHzgwzhXr4Uqci7QAYz2OcVI+iwsVU3mGQ33JDVw/n2vyDQ0aP5ETsWyau00WPOZi QVpJlOEHZya/PJVVnsaMspJzg01PjdShFv4Z8CFpmDZwNt7dAbyfpO8xm93UusBmdDwpOP pz/3S2uowu4GJNNXF0+LyO6JNnZV2kreuoLXwXvm4HTeKhxf6DTAZKXImtM3mKFz/CPkI2 eangw2NvM8T7O9SHEt55ADvJqBkQTHGJVBijalfB676aP9/h/Dh+mYRhk8LIxBprK1Prm2 V8Rs5Ltd99c0yq5NQ3UCcY56LWkQeOBEFHDZQ9+ax+7OYPAz2gyWAXFP4vs3Uw== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qarqm-0004Ye-Iz; Tue, 29 Aug 2023 02:05:16 -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 1qarqj-0004QR-4v for emacs-orgmode@gnu.org; Tue, 29 Aug 2023 02:05:13 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qarqX-0006Ky-Jd for emacs-orgmode@gnu.org; Tue, 29 Aug 2023 02:05:06 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-56a55c0f8b1so1972983a12.0 for ; Mon, 28 Aug 2023 23:04:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693289098; x=1693893898; 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=Bpo34j7yd4F+1SMGH8Yhtr3TfrnMeVgX7G+7A7drwqQ=; b=KRD1P/CIdLEcUkRSKt5mHmPdREijbjp3aWAmBE8A1zKxMr0BSL0hf3JYIAx0RUSiHm s6KEsaq0d47aFXG+xFoJdbzMlGJf9xQFND8bWnX4u4feCMaowSL4Qn1LVtEg52jCuXC+ 5HnKANR4KFo+Qm8VvxlrKwFcIw88SnQUAe1qvq1SYUEvNTgvPT49bHJCOqEucnBObvXB MEGpsn4W2hjGkyfhvM4sUFwthWeY2QivD7oyQQ7MF9MJNhCBupKBVQZoXY+abNSgwMNX 8qJmTRcwiUnI77UlgMPinE39n74kkxgg1gwUvHaJ76fJBOhjcQhVmEXtw7j+Fx7YwUeb H2Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693289098; x=1693893898; 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=Bpo34j7yd4F+1SMGH8Yhtr3TfrnMeVgX7G+7A7drwqQ=; b=bEfc/3ERVd8778neXHVVfFAy7X1eNsSDVf9L/NEeWv4Pwu5jEdWQzGySAHFuTW7X48 TH1g6WU1f2dDAF31+E6AVDOM9mGvzysbRhr5KszfIt71i7JVtb7EgU8twS93XZo0QGYM 4UfWC97GiZUdPhHHh+YHjsURQhZT84ZnfQUEOdagRTlclsn5T8S7kuWQO3/NYvt5irU4 MxPWclC9HK4XV7q1Yq03vrSNHFH/tBSPwUxUV0hpkKrL8ZZxL2VE4tMsUv+XkuKd3ZD0 MXOZT+YJ1AEvz1QHlpHyyrdKiFiUuO2I+tnlcu3z9HRPvzztIDC50CcL2bAz/rvFdMvH T1Dg== X-Gm-Message-State: AOJu0YyrpRRv5Ruz3y9OLhesx4Mz3c4sNznFI5OafIXJnwskqPelpxnQ ie36FCdtngqbMIOlAENDS+Orjh7WVUg= X-Google-Smtp-Source: AGHT+IG7ch4a9Jk8akOV4V709CDuNPxtliYHYRRDf5/kbxhDUPMnM0IKo5uOgLbBA+CUUZGmupQMlA== X-Received: by 2002:a05:6a20:6d9d:b0:13d:ac08:6b79 with SMTP id gl29-20020a056a206d9d00b0013dac086b79mr18047782pzb.60.1693289098211; Mon, 28 Aug 2023 23:04:58 -0700 (PDT) Received: from localhost (S01069050ca65e863.ed.shawcable.net. [174.3.244.184]) by smtp.gmail.com with ESMTPSA id d3-20020aa78143000000b0068883728c16sm7899219pfn.144.2023.08.28.23.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 23:04:57 -0700 (PDT) From: Roshan Shariff To: emacs-orgmode@gnu.org Cc: Roshan Shariff Subject: [PATCH 1/1] Fix background color when line-wrapping latex previews Date: Tue, 29 Aug 2023 00:01:02 -0600 Message-ID: <20230829060145.45738-3-roshan.shariff@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <87lek2up0w.fsf@tec.tecosaur.net> References: <87lek2up0w.fsf@tec.tecosaur.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=roshan.shariff@gmail.com; helo=mail-pg1-x532.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-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx2.migadu.com X-Migadu-Spam-Score: -6.09 X-Spam-Score: -6.09 X-Migadu-Queue-Id: 8F8BA50707 X-TUID: 1shJWB2xSo78 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 org-block has :extend t, its background color is used for the empty space after the end of the line preceding a wrapped latex preview. This is because when Emacs displays the empty space, it only considers faces with a non-nil :extend attribute. If the text surrounding the latex code has a face without :extend, then its background color is ignored for the end-of-line empty space; the latex code face is used instead. We want the empty space to use the background color of the surrounding text if it sets :extend, or the default background color otherwise. To accomplish this, we create a new anonymous face that inherits from the default face and has the :extend attribute, and set it as a fallback to the face property gleaned from the surrounding text. This fallback will be used only if the text surrounding the latex code doesn't set the :extend attribute. It has no effect on anything other than the empty space. --- 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..39681aace 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 consp) + (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