emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Roshan Shariff <roshan.shariff@gmail.com>
To: emacs-orgmode@gnu.org
Cc: Roshan Shariff <roshan.shariff@gmail.com>
Subject: [PATCH 1/1] Fix background color when line-wrapping latex previews
Date: Tue, 29 Aug 2023 00:01:02 -0600	[thread overview]
Message-ID: <20230829060145.45738-3-roshan.shariff@gmail.com> (raw)
In-Reply-To: <87lek2up0w.fsf@tec.tecosaur.net>

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



  parent reply	other threads:[~2023-08-29  6:06 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-12 12:23 [Pre-PATCH] Overhaul of the LaTeX preview system Timothy
2023-03-13 12:26 ` Ihor Radchenko
2023-03-14 16:04   ` Timothy
2023-03-15  9:34     ` Ihor Radchenko
2023-03-15 17:28       ` Rudolf Adamkovič
2023-03-13 12:30 ` Ihor Radchenko
2023-03-13 12:32   ` Ihor Radchenko
2023-03-14 14:46     ` Timothy
2023-03-13 12:36 ` Ihor Radchenko
2023-03-14 16:10   ` Timothy
2023-03-15  9:38     ` Ihor Radchenko
2023-03-13 12:37 ` Ihor Radchenko
2023-03-14 16:13   ` Timothy
2023-03-25 15:29 ` Tony Zorman
2023-05-02 12:15 ` K. Chousos
2023-05-03 10:07 ` K. Chousos
2023-05-05 17:57   ` Karthik Chikmagalur
2023-05-05 18:48     ` Konstantinos Chousos
2023-05-05 18:53     ` Konstantinos Chousos
2023-05-05 20:05       ` Karthik Chikmagalur
2023-05-05 21:06         ` Konstantinos Chousos
2023-05-05 22:58           ` Karthik Chikmagalur
2023-05-06  9:19             ` Konstantinos Chousos
2023-05-06  9:30               ` Ihor Radchenko
2023-05-06 10:08               ` Konstantinos Chousos
2023-05-08 20:04               ` Karthik Chikmagalur
2023-05-08 20:40                 ` Konstantinos Chousos
2023-05-09 11:34                 ` Ihor Radchenko
2023-05-09 12:03                   ` Timothy
2023-05-09 12:10                     ` Ruijie Yu via General discussions about Org-mode.
2023-05-09 12:12                     ` Ihor Radchenko
2023-05-09 12:10                       ` Timothy
2023-05-09 12:49                   ` Max Nikulin
2023-05-09 12:57                     ` Ihor Radchenko
2023-05-10 10:07 ` Jun Inoue
2023-05-10 10:21   ` Timothy
2023-05-10 13:09     ` Jun Inoue
2023-05-26  9:50 ` Rudolf Adamkovič
2023-05-28 17:31   ` Timothy
2023-06-03  9:00     ` Rudolf Adamkovič
2023-08-21  8:59 ` Visuwesh
2023-08-21 16:40   ` Karthik Chikmagalur
2023-08-21 17:19     ` Visuwesh
2023-08-21 21:40       ` Karthik Chikmagalur
2023-08-22  2:57         ` Visuwesh
2023-08-29  6:01 ` Roshan Shariff
2023-08-29  6:01 ` Roshan Shariff [this message]
2023-08-29 17:14   ` [PATCH v2] Fix background color of latex previews Roshan Shariff
2024-01-07  7:25     ` Timothy
2024-01-07  9:50       ` Roshan Shariff
2024-01-08 10:24         ` Timothy
2024-01-09 20:12           ` Roshan Shariff
2024-01-07  7:28 ` [Pre-PATCH] Overhaul of the LaTeX preview system Timothy
2024-01-20  5:53 ` Matt Huszagh
2024-01-20  6:02 ` Matt Huszagh
2024-01-20  7:13   ` Matt Huszagh
2024-01-21 17:08     ` Timothy
2024-04-22 11:08 ` Feedback on the new "feature" system in org-export (was: [Pre-PATCH] Overhaul of the LaTeX preview system) Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230829060145.45738-3-roshan.shariff@gmail.com \
    --to=roshan.shariff@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).