* lisp/org-latex-preview.el (org-latex-preview-live--src-buffer-setup):
Ensure that live LaTeX previews of org-src buffer contents are created
in the src buffer. Since this runs on a timer, the active buffer can be
different from the org-src LaTeX buffer when the preview run fires.
Ensure that `org-element-context' is not called from the org-src buffer
by setting the element type persistently when it is set up.
* lisp/org-latex-preview.el (org-latex-preview-cache-images,
org-latex-preview--colors-around): Add
`org-latex-preview-cache-images', which produces preview images
synchronously for all applicable LaTeX fragments in a parse-tree
and provides a hash table containing this information. This is
intended for use with export backends (html, odt) that can display
LaTeX using svg/png LaTeX preview images. The function can also
be used for general scripting purposes, to obtain a list of image
files and metadata for LaTeX fragments in an Org buffer.
*
lisp/org-latex-preview.el (org-latex-preview--check-all-fragments-produced):
When the first fragment in a preview run silently fails to render,
this function tries to cache a nil value as a fragment. Fix by
checking that the fragment that fails to render is valid as a
fragment.
* lisp/org-latex-preview.el (org-latex-preview--tex-styled,
org-latex-preview--dvisvgm-filter,
org-latex-preview--dvisvgm-version>=3.1,
org-latex-preview-process-alist,
org-latex-preview--dvisvgm3-minor-version): Take advantage of the new
--message flag of dvisvgm 3.2 to produce a more concise output.
* lisp/org-latex-preview.el (org-latex-preview--construct-entries,
org-latex-preview-auto--regenerate-overlay): Blank fragments produce
invisible previews, which is at best silly and at worst confusing. It's
much better to simply not produce a preview image of blank fragments.
We also now avoid exessive re-numbering calculations when repeatadly
generating live previews.
* lisp/org-latex-preview.el (org-latex-preview--get-cached): When using
persist, occasionally we need to be more careful about assuming that
cache entries exist.
* lisp/org-latex-preview.el (org-latex-preview--check-all-fragments-produced):
This function assumes that previews are being generated for placement in
the buffer. Handle the case of preview generation for HTML exports.
* lisp/org-latex-preview.el (org-latex-preview--face-around): On Emacs
28 (and presumably below), should a face with :extend set be identified
as appropriate, the overlay may cast a pall (background colour) over the
rest of the line. This can be addressed by replacing the use of the
default face to normalise display with an anonymous face that inherits
from default and has the :extend attribute set.
Reported-by: Roshan Shariff <roshan.shariff@gmail.com>
Link: https://list.orgmode.org/CAG8iPGxoAD7qmKzDXQ_SRFbmxD7jdM96piCa-dCSCpzae4rCsw@mail.gmail.com
* lisp/org-latex-preview.el (org-latex-preview--get-cached,
org-latex-preview--table, org-latex-preview-persist,
org-latex-preview-cache): Rename and tweak `org-latex-preview-persist'
to also allow for caching to a custom directory, and (in the near
future) unify behaviour with HTML export.
* testing/lisp/test-org-latex-preview.el: Accommodate for the cache variable
change.
* etc/ORG-NEWS: Accommodate for the cache variable change.
* lisp/org-latex-preview.el (org-latex-preview-live--src-buffer-setup,
org-latex-preview-auto--maybe-track-element-here,
org-latex-preview-auto-ignored-environments): Introduce a new
customisation variable `org-latex-preview-auto-ignored-environments',
for specifying environments that should not be automatically previewed.
* lisp/org-latex-preview.el (org-latex-preview-live--throttle,
org-latex-preview-live-throttle,
org-latex-preview-live--preview-times,
org-latex-preview-live--update-times,
org-latex-preview-live--record-hook,
org-latex-preview-live--regenerate,
org-latex-preview-live--update-props,
org-latex-preview-live--src-buffer-setup,
org-latex-preview-live--setup, org-latex-preview-live--teardown,
org-latex-preview--create-image-async): Find the optimal throttle
time for live preview updates dynamically. The variable
`org-latex-preview-live--preview-times' records the last three
process run times, and `org-latex-preview-live-throttle' is set to
the average of these. This ensures that there are never more than
two concurrent processes live-previewing a single fragment. Start
another preview run process the end of the throttle period if the
preview image is out of date to ensure that the state of the
preview is eventually consistent with the contents of the
fragment.
* lisp/org-latex-preview.el (org-latex-preview-live--src-buffer-setup):
When live previews are enabled in an Org buffer and using org-src
buffers to edit LaTeX fragments (via `org-edit-special'), show
continuously updated previews in the source Org buffer if it is
visible. If it is not visible, show continuously updated previews
in the org-src buffer.
* lisp/org-latex-preview.el (org-latex-preview-live-display-type,
org-latex-preview-live--display-in-eldoc,
org-latex-preview-live--update-eldoc,
org-latex-preview-live--setup, org-latex-preview-live--teardown):
Add Eldoc support for live previews. The user option
`org-latex-preview-live-display-type' now accepts the symbols
buffer and eldoc as values. When it is set to eldoc and live
previews are enabled, display of the continuously updated previews is
handled by Eldoc. Note that previewing larger images with Eldoc may
require `eldoc-echo-area-use-multiline-p' and `max-mini-window-height'
to be set appropriately.
* lisp/org-latex-preview.el (org-latex-preview-auto-generate,
org-latex-preview-live-preview-inline, org-latex-preview-throttle,
org-latex-preview-debounce, org-latex-preview-live-display-type,
org-latex-preview-live--debounce,
org-latex-preview-live--throttle,
org-latex-preview-live--clearout,
org-latex-preview-live--regenerate,
org-latex-preview-live--update-props,
org-latex-preview-live--ensure-overlay,
org-latex-preview-live--update-overlay,
org-latex-preview-live--setup, org-latex-preview-live--teardown,
org-latex-preview-live--docstring,
org-latex-preview-live--element-type,
org-latex-preview-live--generator,
org-latex-preview--ensure-overlay,
org-latex-preview--failure-callback,
org-latex-preview--svg-make-fg-currentColor): Add support for
live-previewing LaTeX fragments. When
`org-latex-preview-auto-generate' is set to the symbol live and
`org-latex-preview-auto-mode' is turned on, LaTeX previews are
continuously updated when editing a LaTeX fragment. These
previews are shown to the right of or below the fragment. The
preview generation timing can be controlled using the options
`org-latex-preview-debounce' and `org-latex-preview-throttle'.
* lisp/org-latex-preview.el (org-latex-preview-options,
org-latex-preview-appearance-options): Rename
`org-latex-preview-options' to `org-latex-preview-appearance-options',
as it better reflects the purpose of the variable.
(org-latex-preview--tex-styled, org-latex-preview--image-extract-async,
org-latex-preview--create-tex-file,
org-latex-preview--create-image-async, org-latex-preview--hash,
org-latex-preview-place, org-latex-preview--update-overlay): Put the
page width specification in `org-latex-preview-appearance-options', and
adjust existing code to handle this new structuring.
* lisp/ox-html.el (org-html-prepare-latex-images,
org-html-latex-image-options): Adjust for the
`org-latex-preview-appearance-options' rename, and put :page-width in
the options plist, just with a raised value of 1.0 as the HTML default.
* lisp/org.el (org-do-latex-and-related, org-latex-preview-options),
org-compute-latex-and-related-regexp): Adjust for the
`org-latex-preview-appearance-options' rename.
* lisp/org-compat.el: Adjust for the
`org-latex-preview-appearance-options' rename.
* lisp/ob-latex.el (org-babel-execute:latex, org-latex-preview-header)):
Adjust for the `org-latex-preview-appearance-options' rename.
* etc/ORG-NEWS: Adjust for the `org-latex-preview-appearance-options'
rename.
* doc/org-manual.org (Previewing LaTeX fragments): Adjust for the
`org-latex-preview-appearance-options' rename.
* lisp/org-latex-preview.el (org-latex-preview--update-overlay):
Add LaTeX compilation errors to the help-echo property when the
cursor is inside the fragment that is being previewed. This
primarily affects the use of `org-latex-preview-auto-mode`.
* lisp/org-latex-preview.el (org-latex-preview--face-around): To work
around issues where syntax highlighting of the underlying LaTeX fragment
interferers with the foreground colour of the preview, we append default
to the face list regardless.
(org-latex-preview--ignored-faces): While we're improving face
heuristics, it's also come to my attention that whitespace-mode can
interferer with face selection, and whitespace-mode should also be
ignored. Perhaps this should become part of the API?
* lisp/org-latex-preview.el (org-latex-preview): Refactor to support a
symbol-value "mode" argument as well as prefix arguments. This stops
forcing programmatic invocations from having to use uninformative
values, and actually makes the implementation slightly nicer to read I
think.
* lisp/org.el: Change the argument in the preview startup invocation to 'buffer.
* lisp/org-latex-preview.el (org-latex-preview--get-display-dpi): It was
reported that on a WSLg system using pgtk that
`org-latex-preview--get-display-dpi' produced an arithmetic overflow
error. Inspecting the calculation performed and docstrings of functions
involved, it seems we need to be more careful of the `display-mm-height'
output. We now guard against it being zero or nil, which should make
the DPI calculation more robust.
Simply falling back to a guessed DPI of 140 is non-ideal, but probably
the best we can reasonably do here.
* lisp/org-latex-preview.el (org-latex-preview--place-images): Since the
image generation and placement is asynchronous, it is possible that in
the meantime the target overlay or even buffer could be deleted. To
avoid errors, we simple check that this is not the case before
attempting to place preview images.
* lisp/org-latex-preview.el (org-latex-preview--update-overlay): When
`tooltip-mode' is not active, still show the error, just remove multiple
spaces to be economic with the horizontal room available. This isn't
good as showing the tooltip, but it's better than not showing anything.
* lisp/org-latex-preview.el (org-latex-preview--failure-callback,
org-latex-preview--create-image-async): When the image conversion step
fails due to a missing input file, point the user to the LaTeX output
buffer instead of the image conversion output buffer.
* lisp/org-latex-preview.el (org-latex-preview--image-extract-async,
org-latex-preview--tex-compile-async): Add a comment at the start of the
process output buffers mentioning what the command being run is.
* lisp/org-latex-preview.el (org-latex-preview--format-color,
org-latex-preview--normalize-color): Use three decimal places, and
remove the now-obsolete `org-latex-preview--normalize-color'.
* lisp/org-compat.el (org-normalize-color): With the removal of the
`org-latex-preview--normalize-color' alias, mark `org-normalize-color'
as obsolete.
* lisp/org-latex-preview.el (org-latex-preview--tex-styled,
org-latex-preview--svg-make-fg-currentColor,
org-latex-preview--dvisvgm-filter,
org-latex-preview--dvisvgm-version>=3.1): With dvisvgm 3.1+, it can
replace the foreground color with "currentColor" itself, and so we can
skip `org-latex-preview--svg-make-fg-currentColor' when we are using a
new enough dvisvgm.
(org-latex-preview--await-fragment-existance): Just in case the
asyncronicity in file writing causes issues, we keep the file existence
check (for now).
* lisp/org-latex-preview.el (org-latex-preview--get-numbered-environments,
org-latex-preview--environment-numbering-table): Make numbering
calculations more accurate, at the expense of a ~10x increase in the
time to calculate numbering in a document, mitigated by the use of
caching — resulting in a ~2x improvement compared to the simpler method
in subsequent runs.
(org-latex-preview--numbered-environments,
org-latex-preview--numbered-environments-single,
org-latex-preview--numbered-environments-multi,
org-latex-preview--numbered-environments-all): Split
`org-latex-preview--numbered-environments' into
`org-latex-preview--numbered-environments-single' and
`org-latex-preview--numbered-environments-multi', then rename
`org-latex-preview--numbered-environments' to
`org-latex-preview--numbered-environments-all' for the sake of clarity.
Determining the count of numbered equations in a LaTeX environment is
non-trivial. Previously, a simple heuristic was used that counted the
number of lines ending with "\\" in particular environments, then
subtracted the number of "\nonumber" and "\tag{...}" instances.
However, lines can end with "\\" without contributing to the number of
equations in the environment, consider the "cases" and "bmatrix"
environments. To improve the heuristic, we (with some care) remove
/all/ inner environments from the LaTeX maths environment before
processing. Since this takes more effort, we split the numbered
environments into those we know will contain exactly one numbered
equation where we can avoid the extra processing, and those that require
the more sophisticated heuristics.
The new org-element caching API is very helpful for recovering the
performance cost of this extra computation in the long run.
* lisp/org-latex-preview.el (org-latex-preview--create-image-async,
org-latex-preview--latex-preview-filter,
org-latex-preview--dvipng-filter, org-latex-preview--dvisvgm-filter,
org-latex-preview--place-images): The dvipng and latex-preview filters
run concurrently (interleaved), and both contribute image metadata.
So caching/placing images cannot be associated with the dvipng filter,
this leads to non-deterministic errors where preview images can have
incomplete metadata. To fix this, check if we are using dvipng when
running the LaTeX filter, and place images from whichever of the two
filters processes each fragment second.
Refactor: Move the image placement/caching logic fully to
`org-latex-preview--place-images' instead of duplicating code in all
three (latex-filter, dvipng and dvisvgm) previews.
*
lisp/org-latex-preview.el (org-latex-preview--check-all-fragments-produced):
When rerunning a preview run (because of catastrophic failures), pass
the LaTeX header used to the new process. The current behavior passes
an empty header, which causes the process to fail again.
*
lisp/org-latex-preview.el (org-latex-preview--include-preview-string,
org-latex-preview--latex-preview-filter): when xetex is used,
preview.sty assumes PDF output by default and does not write any
postscript data to the output XDV file. This makes dvisvgm unable to
correctly determine the preview geometry. Fix by specifying the
`dvips' option to preview, which adds (apparently as a side effect)
this geometry information to the XDV file.
Remove a conditional in `org-latex-preview--latex-preview-filter' that
was previously used to attempt to work around this issue.
* lisp/ox-latex.el (org-latex-make-preamble): If xelatex or
lualatex are used for LaTeX export, disable LaTeX header
precompilation (via the CTAN package mylatexformat) locally in the
buffer and issue a warning. Xelatex does not support
precompilation, and while Lualatex supports it in some cases, it
is best left unsupported by the Org LaTeX export process until
precompilation support for them improves upstream.
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file):
Make the same changes as above when precompiling LaTeX headers
for LaTeX preview purposes.
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--latex-preview-filter,
org-latex-preview--create-tex-file,
org-latex-preview-use-precompilation, org-latex-preview--precompile):
Rename (i) the user option that controls precompilation for LaTeX
previews, and (ii) the function that precompiles headres for LaTeX
previews. This makes the names shorter and consistent with their
corresponding equivalents in `ox-latex'.
Rename
`org-latex-preview-use-precompilation' -> `org-latex-preview-precompile'
`org-latex-preview-precompile' -> `org-latex-preview--precompile'
The new names have equivalents `org-latex-precompile' and
`org-latex--precompile' in `ox-latex'.
* testing/lisp/test-org-latex-preview.el: Update tests that set this
user option.
* etc/ORG-NEWS: Update the NEWS file.
* lisp/org-latex-preview.el (org-latex-preview-clear-cache,
org-latex-preview--create-tex-file,
org-latex-preview--include-preview-string): LaTeX headers that are
precompiled include additional strings that set the
textwidth (according to `org-latex-preview-width') and include
preview.sty. These strings are not appended to the header when
trying to clear the precompile cache for the present buffer. Fix
by (i) removing the textwidth declaration from the precompiled
text, and (ii) including the preview.sty inclusion string with the
header we look for when trying to remove the corresponding fmt
file in the org-persist cache.
org-latex-preview--include-preview-string: Introduce string that
holds the text to include preview.sty in the document header.
*
lisp/org-latex-preview.el (org-latex-preview-auto--close-previous-overlay):
Reduce the delay when re-rendering fragments (with `org-latex-preview-auto-mode' turned
on) from 50 ms to 10 ms. This delay is a heuristic number for
perceived smoothness, and this change represents a tuning effort.
* lisp/org-latex-preview.el (org-latex-preview--hash): Use only the
`:scale' parameter from `org-latex-preview-options' when hashing
fragments. Other parameters like `:zoom' do not affect the content of
the image, so this helps avoid building up garbage in the org-persist
cache. `org-latex-preview' is also faster since re-rendering images
is required less often.
*
lisp/org-latex-preview.el (org-latex-preview-auto--close-previous-overlay,
org-latex-preview-auto--open-this-overlay,
org-latex-preview-close-hook, org-latex-preview-open-hook): Change
`org-latex-preview-open-hook' and `org-latex-preview-close-hook' to
abnormal hooks that accept an overlay as the argument. This makes it
easier to apply overlay-specific changes -- which is what these hooks
are primarily for -- without having to find the overlay by probing the
buffer first.
Rename:
`org-latex-preview-open-hook' to `org-latex-preview-open-functions'
`org-latex-preview-close-hook' to `org-latex-preview-close-functions'
* lisp/org-latex-preview.el (org-latex-preview-clear-cache): When
clearing the preview cache, clear the cached preamble after clearing
the image cache, since the preamble is used to compute the image
hashes.
* lisp/org-latex-preview.el (org-latex-preview--ensure-overlay,
org-latex-preview--update-overlay): When ensuring a preview overlay
exists, do not fully reset the state of the overlay, such as the
stored image spec, the flag that checks if the preview is open, etc.
Persisting prior state is required for the "live" preview
feature (introduced in the following commits) to function. Similarly,
if "live" previewing a LaTeX element that has an overlay attached, do
not set the overlay face or error display indicator(s).
* lisp/org-latex-preview.el (org-latex-preview-process-finish-functions,
org-latex-preview-update-overlay-hook, org-latex-preview-close-hook,
org-latex-preview-open-hook, org-latex-preview--create-image-async,
org-latex-preview-auto--handle-post-cursor,
org-latex-preview--run-finish-functions): Add four hooks for better
control of the LaTeX preview process.
`org-latex-preview-process-finish-functions': Runs after the image
conversion process. This is an abnormal hook: each hook function
accepts the same arguments as other `org-async' callbacks for this
process.
`org-latex-preview-close-hook' and `org-latex-preview-open-hook':
Run after a preview is closed (text hidden to reveal image) or
opened (image hidden to reveal text) respectively.
`org-latex-preview-update-overlay-functions': Run after a LaTeX
preview overlay is updated with a new image. This is an abnormal
hook: each hook function accepts one argument, the overlay that was
updated.
The "live" preview system introduced in the following commits uses
these hooks, along with the facilities provided by
`org-latex-preview-auto-mode'. In addition, these hooks will
prove useful when writing code that reuse the API afforded by
`org-latex-preview-place' to provide LaTeX previews for other
major modes.
* lisp/org-latex-preview.el (org-latex-preview-auto-blacklist): Add a
blacklist for commands that should not cause LaTeX preview overlays to
be "opened", i.e. for preview images to be hidden and text revealed when
using `org-latex-preview-auto-mode'. A common use case would be cursor
navigation commands like `next-line' or `scroll-up-command' that are
often intended to move the view of the buffer, where opening preview
overlays can be distracting.
*
lisp/org-latex-preview.el (org-latex-preview-auto--regenerate-overlay):
When `org-latex-preview-auto-mode' is active and a LaTeX fragment
or environment boundary is deleted,
`org-latex-preview-auto--regenerate-overlay' generates a new
preview for the new org-element at point, which is no longer a
LaTeX fragment or environment. This results in paragraph text or
other syntax being captured into the preview. Fix by adding a
check to ensure that the element post-change is still a LaTeX
fragment or environment.
* lisp/org-latex-preview.el (org-latex-preview-auto--marker,
org-latex-preview-auto-mode): `org-latex-preview-auto--marker' is
initialized to a marker, which can cause it to not be
buffer-local. Instead, initialize it to nil and explicitly set it
to a marker when running `org-latex-preview-auto-mode'.
* lisp/org-latex-preview.el (org-latex-preview--get-preamble):
When `org-startup-with-latex-preview' (or the `#+startup:
latexpreview') option is set, preamble generation and
precompilation fail as latex previews are generated in the clone
of the current buffer created by `org-export-with-buffer-copy'.
This is because org-mode is turned on in the cloned buffer, which
requires creating LaTeX previews there. Not only is this
unnecessary, it causes a flurry of errors since the cloned buffer
is killed before the previews can be placed. Explicitly avoid
org-startup-* tasks in the cloned buffer when generating the LaTeX
preamble for our main Org buffer.
* lisp/org-latex-preview.el (org-latex-preview-clear-cache): When
running `org-latex-preview-clear-cache', the precompiled format
file is only removed from the `org-persist' cache if the preamble
has been cached in the current buffer. We require the format file
to always be unregistered from the cache, so generate the preamble
if necessary so it can be hashed and used as the key to find the
corresponding format file in the cache.
*
lisp/org-latex-preview.el (org-latex-preview-auto--mark-overlay-modified):
Reduce slightly the number of checks needed to run each time text
inside a LaTeX fragment overlay is modified.
* lisp/org-latex-preview.el (org-latex-preview--latex-preview-filter):
If during LaTeX compilation hyperref seems to be present, emit a warning
mentioning the sizing issue it is known to cause.
* lisp/org-latex-preview.el (org-latex-preview--tex-styled,
org-latex-preview-numbered): Allow for preview-order based equation
numbering, by setting `org-latex-preview-numbered' to the symbol
"preview".