Compare commits

..

151 Commits

Author SHA1 Message Date
TEC 3edac8fcc6
---END PERSONAL NOPUSH DIVIDER---
--
2.42.0
2024-01-30 13:37:20 +08:00
TEC ed9893898f
NOPUSH org: Don't fill displayed equations in text
* list/org.el (org-fill-element): If a displayed equation (\[ ... \])
starts on its own line, it should not be filled into the rest of the
text. I.e.,

some nice text
\[
  1+1=2
\]
more text.

should not become,

some nice text \[ 1+1=3 \] more text.

While the above example may not look bad, with non-trivial equations
this can become quite messy.
2024-01-30 13:37:20 +08:00
TEC 579f748af9
NOPUSH org-src: Prettify inline results
* lisp/org.el (org-inline-src-prettify-results):

* lisp/org-src.el (org-fontify-inline-src-blocks-1):
2024-01-30 13:37:20 +08:00
TEC c9a8f74cbe
---NOPUSH PERSONAL DIVIDER---
--
2.42.0
2024-01-30 13:37:20 +08:00
TEC f308ecfbab
org-latex-preview: Take advantage of dvisvgm3.2
* 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.
2024-01-30 13:37:12 +08:00
TEC b49cefc7ea
ox-html: Allow inlining svg tex as <svg> elements
* lisp/ox-html.el (org-html-latex-image--data, org-html-latex-image):
Add support for inlining dvisvgm-exported svgs as <svg> elements,
preserving the effect of the currentColor attribute.  While making this
change, we improve a few other aspects of the html LaTeX fragment export
system, namely refactoring/code style improvements and the recognition
of \[...\] and $$...$$ fragments as blocks not inline fragments.
2024-01-30 13:37:12 +08:00
TEC 8284cec27e
org-latex-preview: Skip blank fragments
* 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.
2024-01-30 13:37:12 +08:00
TEC fee79ce34f
org-latex-preview: Accommodate a flaky cache
* 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.
2024-01-30 13:37:12 +08:00
TEC 775ed43c90
org-latex-preview: Tweak to be rebased later
* lisp/org-latex-preview.el (org-latex-preview--cache-image):
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 43b925a712
org-latex-preview: Handle html export preview fail
* 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.
2024-01-30 13:37:12 +08:00
TEC 5ee791d0af
org-latex-preview: Fix face selection Emacs 28 bug
* 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
2024-01-30 13:37:12 +08:00
TEC 21476abcce
ox-html: Support for customised latex image dir
* lisp/ox-html.el (org-html-latex-image, org-html-prepare-latex-images,
org-html-latex-image-options): Allow for customising the LaTex image
directory in `org-html-latex-image-options'.
2024-01-30 13:37:12 +08:00
TEC dccc392855
org-latex-preview: Rework general cache variable
* 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.
2024-01-30 13:37:12 +08:00
TEC f9772da05c
org-latex-preview: Add ignored auto environments
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 45cedee0a9
org-latex-preview: Dynamic throttle for live previews
* 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.
2024-01-30 13:37:12 +08:00
TEC 72b829cd90
org-latex-preview: Adjust customisation scheme
* lisp/org-latex-preview.el (org-latex-preview-live--teardown,
org-latex-preview-live--setup, org-latex-preview-live--src-buffer-setup,
org-latex-preview-live--ensure-overlay, org-latex-preview-throttle,
org-latex-preview-debounce, org-latex-preview-live-preview-inline,
org-latex-preview-auto--open-this-overlay,
org-latex-preview-auto--detect-fragments-in-change,
org-latex-preview-auto--handle-post-cursor,
org-latex-preview--face-around, org-latex-preview-auto-generate,
org-latex-preview-auto-blacklist, org-latex-preview-clear-cache,
org-latex-preview--latex-preview-filter,
org-latex-preview--create-tex-file,
org-latex-preview--create-image-async, org-latex-preview--hash,
org-latex-preview--auto-aware-toggle, org-latex-preview--preview-region,
org-latex-preview-live--teardown, org-latex-preview-live--setup,
org-latex-preview-live--src-buffer-setup,
org-latex-preview-live--ensure-overlay,
org-latex-preview-auto--regenerate-overlay,
org-latex-preview-auto--close-previous-overlay,
org-latex-preview-auto--open-this-overlay,
org-latex-preview-auto--detect-fragments-in-change,
org-latex-preview--update-overlay,
org-latex-preview--indicate-processing, org-latex-preview-precompile,
org-latex-preview-open-functions, org-latex-preview-close-functions,
org-latex-preview-update-overlay-functions,
org-latex-preview-auto-command-blacklist,
org-latex-preview-processing-indicator,
org-latex-preview-default-process): Change the customisation naming
scheme to be more consistent.

* testing/lisp/test-org-latex-preview.el: Change the LaTeX preview
customisation naming scheme to be more consistent.

* lisp/org-compat.el: Change the LaTeX preview customisation naming
scheme to be more consistent.

* etc/ORG-NEWS: Change the LaTeX preview customisation naming scheme to
be more consistent.

* doc/org-manual.org (Footnotes, Previewing LaTeX fragments): Change the
LaTeX preview customisation naming scheme to be more consistent.
2024-01-30 13:37:12 +08:00
TEC 4e9d89a431
ox-latex: Introduce \codefont command
* lisp/ox-latex.el (org-latex-engraved-preamble): Make it easier to
customise the font used in engraved Code environments with a \codefont
command (defaulting to \footnotesize).
2024-01-30 13:37:12 +08:00
TEC ea636c27d2
ox-latex: Use more robust engraved theme grouping
* lisp/ox-latex.el (org-latex-src--engrave-code): Use of curly brackets
for grouping has a few edge-cases which \begingroup-\endgroup should not
suffer from.
2024-01-30 13:37:12 +08:00
TEC 96c0c34875
ox-latex: Better handle multiple engraved themes
* lisp/ox-latex.el (org-latex-generate-engraved-preamble): Improve the
generated LaTeX code in the multiple-themes case. Using "\long\def"
instead of "\newcommand" mixed with "\renewcommand" is needed without
without duplicating the default theme.  The "\efstrut" command also
needs to be extracted out of the theme-generated commands, which was
previously overlooked.
2024-01-30 13:37:12 +08:00
TEC cf3c6152ee
ox-latex: Fix engraved-theme propagation
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src--engrave-code, org-latex-inline-src-block--engraved):
Actually propagate the ":latex-engraved-theme" property through to the
`org-latex-src--engrave-code` call.  To account for whether the theme is
the default theme, or whether it will need to be explicitly switched to,
a new indicator argument ("explicit-theme-p") has been added to
`org-latex-src--engrave-code`.
2024-01-30 13:37:12 +08:00
TEC 34891ff59e
ol: more versatile #+link replacement forms (WIP)
* lisp/ol.el (org-link-expand-abbrev, org-link--abbrev-functions): Allow
link abbreviations to use %s and %h as format-specs, as well as allowing
an anonymous function form with %(sexpr using tag).
2024-01-30 13:37:12 +08:00
TEC 07545b4ca4
org: Introduce semantic seperator fontification
* lisp/org.el (org-fontify-semantic-seperator,
org-set-font-lock-defaults): TODO
2024-01-30 13:37:12 +08:00
TEC 5553b040fb
ox-html: Fix C++ label CSS
* lisp/ox-html.el (org-html-style-default): The "C++" pre-label works if
the "+" characters are backslash-escaped.
2024-01-30 13:37:12 +08:00
TEC 34f0dd395d
---PATCH SET DIVIDER---
--
2.42.0
2024-01-30 13:37:12 +08:00
TEC 5b6b97a264
org-latex-preview: Formatting and spacing tweaks
* lisp/org-latex-preview.el (org-latex-preview--dvipng-filter,
org-latex-preview--latex-preview-filter,
org-latex-preview--create-tex-file,
org-latex-preview--create-image-async,
org-latex-preview--get-numbered-environments,
org-latex-preview-live--setup,
org-latex-preview-live--update-overlay-run,
org-latex-preview-auto--detect-fragments-in-change,
org-latex-preview--update-overlay): Adjust formatting/spacing.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 5caf6d9e91
org-latex-preview: Live previews in org-src bufs
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur bf3f7e3c02
org-latex-preview: Eldoc support for live previews
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur d4d050380a
org-latex-preview: Add support for live previews
* 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'.
2024-01-30 13:37:12 +08:00
TEC d4d644c2e2
---LIVE PREVIEW DIVIDER--- 2024-01-30 13:37:12 +08:00
TEC 1652e5402c
org-latex-preview: Update copyright years 2024-01-30 13:37:12 +08:00
TEC 45de17ef08
org-latex-preview: Put :page-width in opt plist
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 787508d284
org-latex-preview: Fix preview help-echo property
* 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`.
2024-01-30 13:37:12 +08:00
TEC 0c5b3c29c7
org-latex-preview: Improved face heuristics
* 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?
2024-01-30 13:37:12 +08:00
TEC 1354b2d084
org-latex-preview: Support symbol entrypoint args
* 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.
2024-01-30 13:37:12 +08:00
TEC eddf27e32f
org-latex-preview: Robustify DPI calculation
* 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.
2024-01-30 13:37:12 +08:00
TEC a76be67517
org-latex-preview: Guard against deleted buf/ov
* 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.
2024-01-30 13:37:12 +08:00
TEC 90bee079e0
org-latex-preview: Still show errs with no tooltip
* 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.
2024-01-30 13:37:12 +08:00
TEC 9a36397a75
org-macs: Set some process variables in org-async
* lisp/org-macs.el (org-async-call): When eking out maximum performance
from external commands, there are a few potentially important process
variables. `org-async-call' is extended to support setting these, with
default values that differ from Emacs' defaults but should enable better
performance in general.
2024-01-30 13:37:12 +08:00
TEC d3c1243a60
org-latex-preview: Mention latex buffer if no dvi
* 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.
2024-01-30 13:37:12 +08:00
TEC dd4d16b886
org-latex-preview: Include running command in bufs
* 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.
2024-01-30 13:37:12 +08:00
TEC 6259461f04
org-latex-preview: Simplify color formatting
* 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.
2024-01-30 13:37:12 +08:00
TEC a8f3357bff
org-latex-preview: Use dvisvgm 3.1+ currentcolor
* 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).
2024-01-30 13:37:12 +08:00
TEC f99b1a320a
org-latex-preview: Improve env numbering calc
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 2b40ffef8d
org-latex-preview: Fix process filter timing errs
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 65bb5ed444
org-latex-preview: Fix LaTeX header on fails + reruns
*
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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 3fc5787831
org-latex-preview: Fix xelatex preview image sizes
*
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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur c185a6dddb
ox-latex: Disable precompile for xelatex/lualatex
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 934a68eb26
org-latex-preview: Rename precompilation vars
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur ab1229e88b
org-latex-preview: Fix bug clearing precompile cache
* 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.
2024-01-30 13:37:12 +08:00
Karthik Chikmagalur 784d7caa3c
org-latex-preview: Reduce delay when re-rendering
*
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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur ef3788cc65
org-latex-preview: Change preview hashing parameters
* 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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 3bf3baf4ab
org-latex-preview: Change hooks to abnormal hooks
*
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'
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 8af00eba7e
org-latex-preview: Fix clear-cache command
* 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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 08836fb076
test-org-latex-preview: Add preliminary tests
* testing/lisp/test-org-latex-preview.el: Tests for
org-latex-preview.el.

* testing/examples/org-latex-preview-test.org: Short but
full-featured document with LaTeX fragments to run tests on.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 8c0a05c797
org-latex-preview: Don't fully reset overlay state
* 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).
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 0e95d16a33
org-latex-preview: Add hooks for customization
*
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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 2a52e43301
org-latex-preview: Option to fine-tune preview opening
* lisp/org-latex-preview.el (org-latex-preview-auto-blacklist,
org-latex-preview-auto--open-this-overlay): 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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur d84aff830a
org-latex-preview: Fix overlay boundary deletion bug
*
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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 453363cb77
org-latex-preview: non-buffer-local auto-marker bug
* 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'.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur e10b8b9a02
org-latex-preview: Fix startup preamble generation
* 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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 8274613308
org-latex-preview: Ensure fmt file cache is cleared
* 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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur b4904f8617
org-latex-preview: Optimize overlay-modify hook
*
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.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur ba691a0c12
Update ORG-NEWS
* etc/ORG-NEWS: Add summary of `org-latex-preview' changes.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur d92210ed0e
org-manual: Update LaTeX preview section
* doc/org-manual.org (Previewing LaTeX fragments): Mention newly
added user options and `org-latex-preview-auto-mode'.
2024-01-30 13:37:11 +08:00
TEC e100ec8f26
ox-html: Allow inlining of LaTeX images
* lisp/ox-html.el (org-html-latex-image, org-html-latex-image-options):
Add a new `org-html-latex-image-options' keyword, ":inline", which
controls image inlining in `org-html-latex-image'.
2024-01-30 13:37:11 +08:00
TEC 0ab7cebbe2
org-latex-preview: Emit warning when hyperref seen
* 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.
2024-01-30 13:37:11 +08:00
TEC 23578e25a1
org-latex-preview: Add preview-order eqn numbering
* 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".
2024-01-30 13:37:11 +08:00
TEC c65e9ad180
org-latex-preview: Use icon for non-numbered eqns
* lisp/org-latex-preview.el (org-latex-preview--get-preamble,
org-latex-preview--single-eqn-format): When numbering is turned off (via
`org-latex-preview-numbered'), instead of always showing "(1)" as the
equation number, use a diamond symbol, and in multiline environments
show +offset.  The LaTeX setup for this is stored in the new const
`org-latex-preview--single-eqn-format'.
2024-01-30 13:37:11 +08:00
TEC 5b8286f45b
org-latex-preview: Fix snippet preamble hash
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file,
org-latex-preview--hash, org-latex-preview-place,
org-latex-preview-clear-cache): The only the default preamble was being
hashed, which led to incorrect results across documents.  To correctly
hash the preamble, it is now determined at the start of
`org-latex-preview-place', and used in `org-latex-preview--hash',
`org-latex-preview--create-tex-file', and
`org-latex-preview-clear-cache'.
2024-01-30 13:37:11 +08:00
TEC 530b63464c
org-latex-preview: Set overlay priority to -80
* lisp/org-latex-preview.el (org-latex-preview--ensure-overlay): Setting
the overlay priority to -80 ensures that the background colour of
hl-line-mode's overlays takes priority, improving the visual consistency
of previews in the buffer.
2024-01-30 13:37:11 +08:00
TEC 7824b610f4
ox-html: Update LaTeX export for images and mathml
* lisp/ox-html.el (org-html-latex-environment, org-html-format-latex,
org-latex-to-html-convert-command, org-format-latex-as-html,
org-html-latex-image-options): Introduce new functions and variables to
work directly with the new org-latex-preview and ox-mathml API.

* lisp/org-latex-preview.el (org-format-latex-as-html,
org-latex-preview--tex-styled, org-latex-preview-replace-fragments,
org-latex-to-html-convert-command, org-latex-preview-options,
org-latex-preview-place-image-link): Move all of the HTML-specific
processing out of org-latex-preview.el, and into ox-html.el, and remove
now-obsolete code.

* lisp/org-compat.el (org-place-formula-image, org-html-format-latex):
Relocate a copy of `org-place-formula-image' and `org-html-format-latex'
here, and mark them as obsolete.
2024-01-30 13:37:11 +08:00
TEC 874b7d5ce1
ox-mathml: Refactor mathml export
* lisp/ox-mathml.el: Introduce a new library for managing mathml conversion.

* lisp/org-latex-preview.el (org-create-math-formula,
org-format-latex-mathml-available-p, org-latex-to-mathml-jar-file): Move
the various mathml functions into ox-mathml.

* lisp/org-compat.el: Add compat entries for the various mathml
conversion functions that have now been brought under ox-mathml.

* lisp/ox-odt.el (org-odt-export-as-odf,
org-odt--translate-latex-fragments): Use the new org-mathml functions.
2024-01-30 13:37:11 +08:00
TEC 4cd9d951df
org: Update LaTeX overlay during fontification
* lisp/org.el (org-do-latex-and-related, org-latex-preview-options): To
ensure that LaTeX preview overlays update as the face changes (for
instance, when in a heading that changes level), the face used is
updated during fontification when applicable.  This also ensures that
the correct face is used when JIT fontification is active.
2024-01-30 13:37:11 +08:00
TEC 4685b6e967
org-latex-preview: Improved face adaptation
* lisp/org-latex-preview.el (org-latex-preview--face-around): Instead of
only examining the face immediately before the LaTeX snippet, also look
at the face immediately after before determining the appropriate face to
use.  This is relevant when the prior face is
uninformative (e.g. org-indent) but the latter face contains the
applicable face (e.g. org-list-dt).  Determination of the applicable
face is now performed in the new function
`org-latex-preview--face-around'.
(org-latex-preview--colors-at, org-latex-preview--update-overlay):
Rename `org-latex-preview--colors-at' to
`org-latex-preview--colors-around', and use
`org-latex-preview--face-around' for better face determination.
(org-latex-preview-place, org-latex-preview-clear-cache): Update to
provide an "end" value to `org-latex-preview--colors-around'.
2024-01-30 13:37:11 +08:00
TEC 5395207923
org-latex-preview: Better handle temp/remote files
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--create-tex-file): Before obtaining the (compiled)
preamble, check for the temp-ness and remote-ness of the file, and act
accordingly.  Non-temp remote files are not currently supported, and an
error will now be thrown in such cases.  The temporary file status is
now passed on to `org-latex-preview-precompile' and through to
`org-latex--precompile'.
2024-01-30 13:37:11 +08:00
TEC 146ce64413
ox-latex: Handle preamble temp-ness explicitly
* lisp/ox-latex.el (org-latex--precompile, org-latex-make-preamble):
Instead of detecting relative file includes within
`org-latex--precompile', have an optional argument to signal a file can
be safely compile in the tempdir.  Move the check to
`org-latex-make-preamble' when calling `org-latex--precompile'.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 4a73d55a5b
org-latex-preview: Clean up tex file on failure
*
lisp/org-latex-preview.el (org-latex-preview--create-image-async):
Ensure that the latex-preview cleanup callback runs when the
preview image extraction process fails.  This is to avoid
littering the default-directory with temporary tex files.
2024-01-30 13:37:11 +08:00
TEC 9a0251fd8f
org-latex-preview: Work around XeTeX misbehaviour
* lisp/org-latex-preview.el (org-latex-preview--latex-preview-filter):
Since XeLaTeX seems to misbehave with tightpage, we special-case the
tightpage processing to account for it.
2024-01-30 13:37:11 +08:00
TEC beea15f433
org-latex-preview: Improved error messages
* lisp/org-latex-preview.el (org-latex-preview--image-extract-async,
org-latex-preview--tex-compile-async,
org-latex-preview--create-image-async): Edit the error messages for
LaTeX compilation and image generation failure to be slightly more informative.
2024-01-30 13:37:11 +08:00
TEC 2d96e36f7d
org-latex-preview: Disable buf precompile on error
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file): When
an error occurs during LaTeX precompilation, disable it using
`setq-local' and emit an informative warning message.
2024-01-30 13:37:11 +08:00
TEC bbf5274b85
org-latex-preview: Use stand-in color for SVG fg
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor,
org-latex-preview-place): Instead of guessing the SVG foreground to be
the first color seen, set a static stand-in color that is unlikely to
occur organically.
2024-01-30 13:37:11 +08:00
TEC f6d5440fd9
org-latex-preview: Guard against caching nil-files
* lisp/org-latex-preview.el (org-latex-preview--cache-image): When
caching an image with `org-latex-preview--cache-image', check that the
image path is non-nil.  Should this not be true, a warning message is
emitted suggesting this is a bug that should be reported to the Org
mailing list.
2024-01-30 13:37:11 +08:00
TEC 125ea5af68
org-latex-preview: Get a list of all started tasks
* lisp/org-latex-preview.el (org-latex-preview--create-image-async):
Instead of just returning the main tex-compile task, return a list of
all async tasks created (i.e. the dvipng task, when used).  This will
allow for `org-async-wait-for' to be used on the list to wait until all
processing has completed.
2024-01-30 13:37:11 +08:00
TEC 126000dd22
org-latex-preview: Split out entry construction
* lisp/org-latex-preview.el (org-latex-preview--place-from-elements,
org-latex-preview--construct-entries): Split the entry construction code
into its own function `org-latex-preview--construct-entries'.

This change will make it easier to generate previews during export.
2024-01-30 13:37:11 +08:00
TEC 64da9ff169
org-latex-preview: Construct numbering from tree
* lisp/org-latex-preview.el (org-latex-preview--get-numbered-environments,
org-latex-preview--environment-numbering-table): Extend
`org-latex-preview--get-numbered-environments' and
`org-latex-preview--environment-numbering-table' to accept a parse tree
to operate on as an optional argument.

This will be useful when trying to integrate numbering with exports.
2024-01-30 13:37:11 +08:00
TEC fa1464b76b
org-latex-preview: Wait for SVGs more robustly
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor,
org-latex-preview--dvisvgm-filter): Instead of waiting 0.01s and hoping
that an SVG exists by that point, we can wait and check for the
existance of an SVG within
`org-latex-preview--svg-make-fg-currentColor', and that should reduce
the chance of edge cases where the delay is insufficient.
(org-latex-preview--svg-make-fg-currentColor): Adjust the
`buffer-substring' call to not error when the buffer is empty.
2024-01-30 13:37:11 +08:00
TEC 2da7552b35
org-latex-preview: Move org-format-latex to compat
* lisp/org-latex-preview.el (org-latex-preview-create-image,
org-format-latex): It has become apparent that it is not reasonably
possible to maintain the behaviour of `org-format-latex' and
`org-create-formula-image' using the new image, so instead put the
original implementations in org-compat.
(org-preview-latex-image-directory): Move and mark as obsolete in
org-compat as it has not be used.

* lisp/org-compat.el: Introduce the original `org-format-latex' and
`org-create-formula-image' definitions.  Also move
`org-preview-latex-image-directory' here and mark as obsolete, since it
has ended up being completely unused.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 3ff99452c2
org-latex-preview: Get geometry from preview.sty
* lisp/org-latex-preview.el (org-latex-preview--latex-preview-filter,
org-latex-preview--tex-scale-divisor,
org-latex-preview--dvipng-dpi-pt-factor): Fetch image geometry and
alignment information from the LaTeX compilation output instead of
dvisvgm or dvipng.  This calculation is simpler and should work for all
image converters (including imagemagick) and with all latex
processors (including xelatex and lualatex).

(org-latex-preview--shameful-magic-tex-scaling-factor): New
variable to match image geometry reported by preview.sty and that
computed by dvisvgm.

(org-latex-preview--dvipng-filter, org-latex-preview--dvisvgm-filter,
org-latex-preview--latex-preview-filter,
org-latex-preview--display-info): Adjust to no longer use the old sizing
information.

* lisp/org-latex-preview.el (org-latex-preview-process-alist,
org-latex-preview-create-image, org-latex-preview--display-info,
org-latex-preview--image-extract-async): Remove the :image-size-adjust
keyword, as with the new geometry detection system it is redundant.

We tried also switching dvisgm from "--bbox=preview" to "--exact-bbox",
as it should no longer be needed, but testing found "--exact-bbox" to
cause sizing issues in practice.
2024-01-30 13:37:11 +08:00
TEC 864ec3e0c9
org-latex-preview: Introduce a place-preview arg
* lisp/org-latex-preview.el (org-latex-preview--dvipng-filter,
org-latex-preview--dvisvgm-filter,
org-latex-preview--check-all-fragments-produced,
org-latex-preview--generic-callback,
org-latex-preview--create-image-async, org-latex-preview-place):
Refactor the preview pipeline to use a "place-preview" argument to
indicate that LaTeX previews should be placed after generation.

This change will pave the way for use with export, where the LaTeX
preview should be generated but not previewed.
2024-01-30 13:37:11 +08:00
TEC 3448ec738f
ox-latex: Precompile preamble for export
* lisp/ox-latex.el (org-latex-precompile, org-latex-make-preamble): Make
use of "mylatexformat" to precompile the LaTeX preamble, for faster
exports.
(org-latex--remove-cached-preamble): Add a function to clear a cached
preamble.

* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--get-preamble): Relocate the precompilation
functionality to ox-latex.el, and use the new ox-latex preamble
precompilation API.
(org-latex-preview-clear-cache): Use the new preamble cache clearing
functionality in ox-latex.
2024-01-30 13:37:11 +08:00
TEC f486a5ee18
org-latex-preview: Use org-persist for .fmt cache
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--create-tex-file): Cache compiled ".fmt" format dump
files with org-persist, instead of using predictably named tempfiles.
`org-latex-preview--precompile-preamble' is introduced to seperate out
the compilation process from the org-persist interaction.  As a result
of the new changes, the ".fmt" extension is also returned now, and so it
must be stripped in `org-latex-preview--create-tex-file'.
2024-01-30 13:37:11 +08:00
TEC fe1b32f20e
org-latex-preview: Rework auto toggle behaviour
* lisp/org-latex-preview.el (org-latex-preview--auto-aware-toggle):
Adjust the toggle behaviour to hopefully behave more intuitively.
2024-01-30 13:37:11 +08:00
TEC b54d64f41d
org-latex-preview: org-latex-conditional-features
* lisp/org-latex-preview.el (org-latex-preview--get-preamble):
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 6034fdad7e
org-latex-preview: Reduce verbosity of messaging
* lisp/org-latex-preview.el (org-latex-preview--cleanup-callback,
org-latex-preview--create-image-async, org-latex-preview-place,
org-latex-preview--auto-aware-toggle, org-latex-preview,
org-latex-preview-auto--regenerate-overlay): Do not emit a message
when previewing is successful.  This reduces how "busy" the echo area
is, especially when `org-latex-preview-auto-mode' is on.  When
previewing fails, display a message as before (behaviour unchanged).
2024-01-30 13:37:11 +08:00
TEC 13774486fb
org-latex-preview: Lazy-load, with autoloads
* lisp/org.el: No longer require org-latex-preview.

* lisp/org-latex-preview.el: Require the relevant files, and add
autoload tokens to `org-latex-preview-options',
`org-latex-preview-process-alist' (for backwards compatibility),
`org-latex-preview-auto-mode', `org-latex-preview' (the major likely
entry points), and `org-latex-preview-place` (potential external entry
point).
2024-01-30 13:37:11 +08:00
TEC e503794506
org-latex-preview: Use the conditional preamble
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file,
org-latex-preview--clear-preamble-cache): Instead using a minimal info
export plist, populate it with buffer-specific values so that the
conditional preamble can be appropriately generated, and cache the
result.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 57e7ee0405
org-latex-preview: Speed up texfile creation time
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file):
Speed up texfile creation time by inhibiting fsync and setting the
`coding-system-for-write' explicitly.  Times are reduced by about
40-50%.
2024-01-30 13:37:11 +08:00
TEC 9146941da4
org-latex-preview: Fix spacing before LaTeX envs
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file): Set
the \abovedisplayskip length to zero to remove the default padding.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 9fc61e4b89
org-latex-preview: Fix element start positions
* lisp/org-latex-preview.el (org-latex-preview--place-from-elements,
org-latex-preview-auto--maybe-track-element-here): The org-element
property `:begin' includes any Org keyword text associated with the
element, such as "#+RESULTS:" or "#+ATTR_LATEX:" and these are swallowed
up in the created preview overlays.  Fix by using the `:post-affiliated'
property over `:begin' when it is available.  Note that some
calculations, such as equation numbering, still use the `:begin'
keyword.  These calculations don't involve creating overlays and thus
this is not (expected to be) an issue.
2024-01-30 13:37:11 +08:00
TEC 0635d33702
org-macs: Support blocking on async tasks
* lisp/org-macs.el (org-async--blocking-tasks, org-async-wait-for,
org-async--cleanup-process, org-async--execute-callback): Add support
for waiting for a set of async tasks to complete via the new function
`org-async-wait-for'.
2024-01-30 13:37:11 +08:00
TEC a14b864f8e
org-latex-preview: Redesign preview creation API
* lisp/org-latex-preview.el (org-latex-preview--check-all-fragments-produced,
org-latex-preview--create, org-latex-preview-fragments,
org-latex-preview-auto--regenerate-overlay,
org-latex-preview-auto--detect-fragments-in-change): Split the
org-element processing and the preview creation components of
`org-latex-preview--create` into two new functions, `
org-latex-preview--place-from-elements' and `org-latex-preview-place'.
The latter of these can serve as a convenient public API for creating
fragments.
(org-latex-preview-place, org-latex-preview-create-image-async): To make
`org-latex-preview-place' this easier to use, we also allow the LaTeX
preamble to be provided as an optional argument, and adjust
`org-latex-preview-create-image-async' to add this as the :latex-header
to the processing-info plist.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 0e345223aa
org-latex-preview: Better imagemagick cleanup
* lisp/org-latex-preview.el (org-latex-preview--generic-callback,
org-latex-preview-create-image-async,
org-latex-preview-process-alist): Reuse
`org-latex-preview--cleanup-callback' for imagemagick-based previews.
Additionally, check produced fragments and regenerate them if
necessary.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur d570e0dca7
org-latex-preview: Fix auto-gen at end of overlay
* lisp/org-latex-preview.el (org-latex-preview-auto--maybe-track-element-here):
`overlays-at' does not include overlays that end at the position it is
called on.  This caused auto-generation to not trigger immediately
when manually typing the closing delimiters of a LaTeX fragment. Fix
this by recording the position just before the end of a newly created overlay.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur a724c130d2
org-latex-preview: Respect auto-generate option
* lisp/org-latex-preview.el (org-latex-preview-auto--detect-fragments-in-change):
Respect the user option `org-latex-preview-auto-generate' when
`org-latex-preview-auto-mode' is active.  (This was lost when the
auto-generation code was rewritten.)
2024-01-30 13:37:11 +08:00
TEC 58e2cbc1b4
org-latex-preview: Use appropriate TeX compiler
* lisp/org-latex-preview.el (org-latex-preview--tex-compile-async,
org-latex-preview-process-alist): Introduce %l and %L placeholders for
the LaTeX compiler.
(org-latex-preview-precompile): Record the LaTeX compiler in the
preamble hash, and add %l/%L to the `org-compile-file' format spec.
(org-latex-preview-compiler-command-map): Introduce a new variable to
map each LaTeX compiler name to the command and arguments needed.

Note that due to idiosyncrasies in how the various TeX compilers behave,
pdfLaTeX will likely continue to provide a smoother experience.  In the
future, some work can be done to improve this situation.
2024-01-30 13:37:11 +08:00
TEC 3746b798e4
org-latex-preview: Allow setting persist expiry
* lisp/org-latex-preview.el (org-latex-preview--cache-image,
org-latex-preview-persist-expiry): Introduce a new variable
`org-latex-preview-persist-expiry' which is used within
`org-latex-preview--cache-image' as the expiry value when calling
`org-persist-register' on preview data.
2024-01-30 13:37:11 +08:00
TEC 8a3aebb986
org-latex-preview: Customisable preview width
* lisp/org-latex-preview.el (org-latex-preview--create-tex-file,
org-latex-preview-width ): Introduce a new variable
`org-latex-preview-width' for convenient setting of the preview width,
and use it in the preamble generation.
(org-latex-preview-header): Remove now-redundant content (thanks to the
use of preview.sty).
2024-01-30 13:37:11 +08:00
TEC 1c2061b799
org-latex-preview: Wait for complete SVG file
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor):
Wait until the SVG file is complete before processing it.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 1351cff6eb
org-latex-preview: Custom processing indicator
* lisp/org-latex-preview.el (org-latex-preview-create-image-async,
org-latex-preview--update-overlay, org-latex-preview--ensure-overlay):
These functions are modified to incorporate customizable visual
indication of preview processing.
(org-latex-preview-processing-indicator): User option to customize how
preview processing is indicated visually.  The current options are:
no indication, show a fringe marker, or apply the
face `org-latex-preview-processing-face'.  The fringe marker is
currently set as the default.
(org-latex-preview--indicate-processing): Add function to handle
applying the appropriate processing indicator.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur f4ace4be19
org-latex-preview: Compile previews in default-dir
* lisp/org-latex-preview.el (org-latex-preview--generic-callback,
org-latex-preview--tex-compile-async,
org-latex-preview--create-tex-file): Compile latex fragments in
`default-directory', with output placed in `temporary-file-directory'.
This is to ensure that relative paths in fragments (such as paths to
figures) are parsed correctly.  Note that relative paths in the header
are handled during precompilation instead.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 08c16b034b
org-latex-preview: Rename filename parts uniformly
* lisp/org-latex-preview.el (org-latex-preview-create-image,
org-latex-preview--dvipng-filter, org-latex-preview--generic-callback,
org-latex-preview--do-cleanup,
org-latex-preview--image-extract-async): Adopt the following
convention for parts of file names used in the latex and image
processing: For file with path 'foo', foo-base refers to its base
name (no directory, no extension) and foo-no-ext refers to its full
path but without a file extension.  The third option (no directory
component but with file extension) is not used in org-latex-preview.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 2ac83404ff
org-latex-preview: Precompile with relative paths
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview-process-alist): Change the precompilation parameters
so the created format file includes all files included/input in the
header with paths relative to the original Org buffer.  The
precompilation is carried out by calling latex from the
`default-directory', with the source tex file and destination format
dump file both placed in `temporary-file-directory'.  Additionally the
source tex file for precompilation is deleted if the precompilation is
successful.
2024-01-30 13:37:11 +08:00
TEC 327e63f6ea
org-persist: Add situational write inhibition
* lisp/org-persist.el (org-persist-write, org-persist--inhibit-write): TODO
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur e6c5e45b65
org-latex-preview: Compile latex in default-dir
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--do-cleanup, org-latex-preview--image-extract-async,
org-latex-preview--tex-compile-async,
org-latex-preview--create-tex-file): Format dump-files can be referred
to by their full paths in latex file preambles by adding a space after the "%&"
token.  Using this, we can compile the tex file containing the
previews in the same directory as the org file and handle relative
file references.  The above functions now do the following:
- format dump-file: Compiled and placed in
temporary-file-directory (no change)
- preview tex file: Refers to the format dump file in
temporary-file-directory. Compiled in default-directory, with output to
default-directory.
- image processors: Input (from tex output) in and output to
temporary-file-directory.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur ae1580c34a
org-latex-preview: Indicate errors when using pngs
* lisp/org-latex-preview.el (org-latex-preview--update-overlay,
org-latex-preview--ensure-overlay): When using dvipng, latex errors
cannot be indicated using an error face, and if a preview is generated
it is indistinguishable from a correct one.  Fall back to a fringe
marker in this situation.
2024-01-30 13:37:11 +08:00
TEC 4d70983b88
org-latex-preview: Introduce auto-aware toggling
* lisp/org-latex-preview.el (org-latex-preview--auto-aware-toggle,
org-latex-preview): Toggling of fragments with auto-mode active needs a
bit of work to function as most people would likely expect.  To provide
this more nuanced behaviour a new function
`org-latex-preview--auto-aware-toggle' is created and replaces the prior
"toggle this element" implementation in `org-latex-preview'.
2024-01-30 13:37:11 +08:00
TEC a7f52bbc19
org-latex-preview: Document async image logic
* lisp/org-latex-preview.el (org-latex-preview-create-image-async):
Explain the async call structure/logic with a large code comment.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 32b3350fb0
org-latex-preview: Add error reporting via tooltip
* lisp/org-latex-preview.el (org-latex-preview--update-overlay): Add a
rudimentary interface for viewing compile errors (via tooltip).
2024-01-30 13:37:11 +08:00
TEC 0dc83ae01f
org-persist: Fallback to associated files in write
* lisp/org-persist.el (org-persist-write:file): If the path given in a
file container does not exist, instead of returning nothing return the
associated path if possible.  This fixes an issue where should the
original file be deleted and `org-persist-write-all' be run, then the
file container's association will be set to nil and so when
`org-persist-gc' is run the nil path will cause
org-persist-associated-files:file to produce an error.
2024-01-30 13:37:11 +08:00
TEC 9d6973ac8b
org-latex-preview: Fix face checking
* lisp/org-latex-preview.el (org-latex-preview--resolved-faces-attr,
org-latex-preview--update-overlay): Handle more of the complexity of the
forms of face specifications, and avoid inheriting information from the
`org-indent' face, which is never useful.
2024-01-30 13:37:11 +08:00
TEC 9e8011b335
org-latex-preview: Wait for dvisvgm to write
* lisp/org-latex-preview.el (org-latex-preview--dvisvgm-filter): As
mentioned in the code comment, dvisvgm seems to sometimes lie about when
file content has been written, so we need to work around that by waiting
a little bit.
(org-latex-preview--cleanup-callback): Now that the svg processing is on
a timer, we must be careful not to run the callback to soon.  This isn't
desperately urgent, so we can use a relatively generous timer for this.
2024-01-30 13:37:11 +08:00
TEC 608c3a7bba
org-latex-preview: Adjust defaults
* lisp/org-latex-preview.el (org-latex-preview,
org-latex-to-html-convert-command, org-latex-preview-process-alist):
Adjust the default LaTeX preview parameters to better suit the reworked
preview system.
2024-01-30 13:37:11 +08:00
TEC 9a3f41c24d
org-latex-preview: Equation numbering
* lisp/org-latex-preview.el (org-latex-preview-numbered): Introduce a
new variable to signal whether equations numbering should be calculated
to be correct throughout the document, or not.
(org-latex-preview--numbered-environments): Create a list of known
numbered environments.
(org-latex-preview--environment-numbering-table,
org-latex-preview--get-numbered-environments): Introduce functions which
create a numbering hash table from the buffer.
(org-latex-preview--tex-styled): When an initial equation number is
provided, add LaTeX code to set the counter to apply the numbering.
(org-latex-preview--create,
org-latex-preview-auto--detect-fragments-in-change,
org-latex-preview--create): Make use of the new equation numbering
facilities.
(org-latex-preview--hash): Include first equation number (if applicable)
in the hash.
(org-latex-preview-clear-cache): Use numbering information.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 1bc075de32
org-latex-preview: Simplify overlay regeneration
* lisp/org-latex-preview.el (org-latex-preview--close-previous-overlay,
org-latex-preview--regenerate-overlay): Overlays carry information
about the buffer they belong to, so it is unnecessary to pass this
information explicitly when passing an overlay argument to a function.
2024-01-30 13:37:11 +08:00
TEC d3e297c78b
org-latex-preview: Reuse overlays more
* lisp/org-latex-preview.el (org-latex-preview--create,
org-latex-preview--maybe-track-element-here,
org-latex-preview--make-overlay): Instead of always deleting and
re-creating overlays in a region when wanting to ensure a suitable
overlay exists, we can look at the scanned overlays, check if a suitable
overlay already exists, and re-use it.  To indicate the change in
behaviour, as well as documentation updates
`org-latex-preview--make-overlay' is renamed to
`org-latex-preview--ensure-overlay'.  While we're at it, the
"front-advance" argument is now set when calling `make-overlay', to make
sure that text typed at the beginning of the fragment is not absorbed
into the overlay.
(org-latex-preview--failure-callback): It's possible the overlay
provided could be nil when something goes wrong, in which case do
nothing.
(org-latex-preview--mark-overlay-modified): Move to a separate function
for neatness.
2024-01-30 13:37:11 +08:00
TEC 2453c8a127
org-latex-preview: Better fragment insertion check
* lisp/org-latex-preview.el (org-latex-preview--handle-insert,
org-latex-preview--detect-fragments-in-change, ): Replace the old
checker `org-latex-preview--handle-insert' that was called as a
`post-insert-hook' with a new method of checking for newly created
fragments based on buffer modifications as a `after-change-functions'
hook.  This has a number of benefits, including generally improved
robustness, and the removal of a reliance on font-lock information.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 40fcbd2bd0
org-latex-preview: Previews stored in /tmp
* lisp/org-latex-preview.el (org-latex-preview--get-cached,
org-latex-preview--remove-cached, org-latex-preview--cache-image):
These functions are modified to search in, remove from or add to a
session cache instead of org-persist's cache.
(org-latex-preview-persist-p): User option to turn off or on
persistent caching.
(org-latex-preview--table): Hash table used to hold preview image
metadata when using a session cache instead of org-persist.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 78b94ce83a
org-latex-preview: Fix precomp when backend switch
* lisp/org-latex-preview: (org-latex-preview-process-alist,
org-latex-preview-precompile): Update programs required for
imagemagick conversion to include pdflatex.  Precompilation now
generates a new format file when switching
`org-latex-preview-default-process' to or from imagemagick.
2024-01-30 13:37:11 +08:00
TEC e54dfbf4b4
org-macs: Ensure that filter run before callbacks
* lisp/org-macs.el (org-async--cleanup-process): Run
`accept-process-output` until no further output is fetched on process
exit, so we can assume that the callbacks are run after all filter
invocations.
2024-01-30 13:37:11 +08:00
TEC bd593af82c
org-macs: Add :coding kwarg to org-async-call
* lisp/org-macs.el (org-async-call): It is recommended in
(info "(elisp) Decoding Output") that the process coding system be set.
To allow for this we add a new keyword argument, :coding, and set the
default value to utf-8.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 2bf81ed249
org-latex-preview: Improve preview creation msg
* lisp/org-latex-preview.el: (org-latex-preview,
org-latex-preview-create-image-async): `org-latex-preview' starts an
async process, and so it can only inform the user of the start of the
process. Information about the completion (to success or failure) is
provided by the relevant process sentinels instead.
(org-latex-preview--latex-log, org-latex-preview--image-log,
org-latex-preview--precompile-log): Log buffer names for preview latex
processing.
(org-latex-preview--tex-compile-async,
org-latex-preview--image-extract-async, org-latex-preview-precompile,
org-latex-preview-create-image): Use the newly created log buffer name
variables.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur fa63fc5c8f
org-latex-preview: Clear failed preview overlays
* lisp/org-latex-preview.el: (org-latex-preview-create-image-async): If
preview images fail to generate for some or all LaTeX fragments, the
overlays placed on the fragments during processing need to be cleared.
This is done by adding a (failure) callback function to the image
generation process.
(org-latex-preview--failure-callback): Add callback that clears overlays
for previews that fail to generate.  Other behaviours are possible here
but not implemented yet.  This includes signalling failure by changing
how the overlays are displayed, such as by adding a face or fringe
marker.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur c4a68d7ab4
org-latex-preview: Make clear-cache user-friendly
* lisp/org-latex-preview.el (org-latex-preview-clear-cache): Ask for
confirmation before wiping the entire LaTeX preview cache, and confirm
cache clearing at the end when clearing the buffer cache.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur beba8515ac
org-latex-preview: Add cache clearing function
* lisp/org-latex-preview.el (org-latex-preview-clear-cache): Clears the
latex preview cache for LaTeX fragments in a region.  This can be called
as a command by itself, but is primarily required when previewing
fragments by force-regenerating their preview images.  This might be
necessary when the output image corresponding to a fragment is malformed
in some way, for instance.
2024-01-30 13:37:11 +08:00
TEC 3bc2a4ff01
org-latex-preview: More robust face resolving
* lisp/org-latex-preview.el (org-latex-preview--colors-at,
org-latex-preview--resolved-faces-attr): Just using `face-attribute' on
the first face spec given when the face text property is a list is too
naive.  To do this correctly, a new function
`org-latex-preview--resolved-faces-attr' is introduced which takes care
of the annoying semantics, and used in `org-latex-preview--colors-at' to
better resolve the face attribute.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur b0de57ef71
org-latex-preview: Split org-latex-preview--create
* lisp/org-latex-preview.el (org-latex-preview--create): This function
does many tasks, including computing face colors and hashes for
fragments.  Split this function into three to reuse the parts for other
planned purposes, such as clearing cached images.
(org-latex-preview--colors-at): Finds appropriate foreground and
background colors at a position for the preview image of a LaTeX
fragment.  Also improve the code to handle multiple faces.
(org-latex-preview--hash): Computes the hash key for a LaTeX fragment
combined with relevant processing metadata.  This key is used to
identify the fragment when caching and placing images.
2024-01-30 13:37:11 +08:00
TEC f232aab63f
org-latex-preview: Graceful precompile failure
* lisp/org-latex-preview.el (org-latex-preview-precompile,
org-latex-preview--create-tex-file): Fall back to compiling without
using a preamble format file if the preamble could not be precompiled
without error.  In such an event, alert the user that preamble
precompilation failed, and in particular let them know if mylatexformat
could not be found.
2024-01-30 13:37:11 +08:00
TEC 3ce9f76703
org-latex-preview: Check all fragments produced
* lisp/org-latex-preview.el (org-latex-preview--check-all-fragments-produced):
Create a new callback that checks that each fragment has indeed been
produced not, and if not guesses the last produced fragment caused a
problem.  The last produced fragment is then marked as erroneous,
its cache entry updated (replaced and re-inserted), and the remaining
fragments are regenerated.
(org-latex-preview-create-image-async): Run
`org-latex-preview--check-all-fragments-produced' after successfully
extracting images.
(org-latex-preview--cache-name, org-latex-preview--cache-image,
org-latex-preview--get-cached): Use a defconst instead of hardcoding the
cache container string in `org-latex-preview--cache-image' and
`org-latex-preview--get-cached'.
(org-latex-preview--remove-cached): Easy removal of an item from the
org-persist cache, as a counterpart to `org-latex-preview--cache-image'.
2024-01-30 13:37:11 +08:00
TEC 5c40409f64
org-latex-preview: Strip redundant color switching
* lisp/org-latex-preview.el (org-latex-preview--create,
org-latex-preview--create-tex-file): Keep track of the last fragment's
foreground/background color, and only include LaTeX color specifications
as needed.
(org-latex-preview-create-image-async): Provide the processing type so
`org-latex-preview--create-tex-file' can change the style of color
setting as appropriate.  This allows the dvipng route to use postscript
special statements instead of regular color declarations.
2024-01-30 13:37:11 +08:00
TEC 57e205306c
org-latex-preview: Do not show empty SVG fragments
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor):
When an SVG is empty, delete it.  This will cause
`org-latex-preview--cache-image' to give nil as the path.  When the
provided path is nil, do nothing.
(org-latex-preview--update-overlay): If the image path is nil, do
attempt to place an image.  If an error is recorded and there is no
image this causes the text to be shown with the error face.
(org-latex-preview--cleanup-callback): If the image path is nil, do not
try to delete it.
2024-01-30 13:37:11 +08:00
TEC 13e7d02b84
org-persist: Allow inhibiting of normalisation
* lisp/org-persist.el (org-persist-register,
org-persist--normalize-container,
org-persist--inhibit-container-normalization): Since container
normalisation is applied frequently (via
`org-persist--normalize-container'), when registering many new
containers the cost can quickly add up.  To avoid redundant overhead,
after normalising the container initially in `org-persist-register' we
bind a new variable `org-persist--inhibit-container-normalization' to t
and adjust `org-persist--normalize-container' to do nothing when this
new variable is non-nil.
2024-01-30 13:37:11 +08:00
TEC 0db9f1e0de
org-latex-preview: Cache formatted color strings
* lisp/org-latex-preview.el (org-latex-preview--format-color,
org-latex-preview--format-color-cache): Introduce a color cache alist, and use
it.
2024-01-30 13:37:11 +08:00
TEC 1aa997dd57
org-latex-preview: Reduce overhead in SVG editing
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor):
By setting the coding system and avoiding triggering any file
handlers (such as the `image-file-handler'), memory usage is nearly
halved, reducing GC pressure.
2024-01-30 13:37:11 +08:00
TEC b1f9c0bfc8
org-latex-preview: Tweak baseline alignment
* lisp/org-latex-preview.el (org-latex-preview--update-overlay): After
doing quite a bit of testing, tweak the ascent formula to introduce a
slight correction to visually improve the final result.
2024-01-30 13:37:11 +08:00
TEC fd029fc666
org-latex-preview: Introduce dvipng filter
* lisp/org-latex-preview.el (org-latex-preview--dvipng-filter,
org-latex-preview-create-image-async): Introduce a dvipng filter
function to capture information from the dvipng stdout, and place images
as they are generated.
(org-latex-preview-process-alist): Update default dvipng process to emit
depth and height information.
2024-01-30 13:37:11 +08:00
TEC c78812fb8f
org-latex-preview: Eagerly make fragment overlays
* lisp/org-latex-preview.el (org-create-latex-export,
org-latex-preview--place-images, org-latex-preview--generic-callback,
org-latex-preview--create-tex-file,
org-latex-preview-create-image-async, org-latex-preview--create,
org-latex-preview--close-previous-overlay,
org-latex-preview--make-overlay, org-latex-preview--update-overlay,
org-latex-preview-processing-face): Instead of saving buffer locations
and creating an overlay after the fragment image has been generated,
create an overlay initially and then update it when the image has been
created.  This is done by splitting the image-related code of
`org-latex-preview--make-overlay' into a new function,
`org-latex-preview--update-overlay'.  This provides a number of
advantages, primarily robustness to buffer edits during image
generation.  It also allows us to create a face to indicate
in-progress (`org-latex-preview-processing-face'), which is a nice
visual indicator.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 115f3f59a1
org-latex-preview: Avoid duplicating cached images
* lisp/org-latex-preview.el (org-latex-preview--cache-image): When
caching an image, check if there is already an entry in the
`org-persist--index' with the same hash as this one.  This avoids
missing preview images for fragments with duplicates in a buffer, or
across org-persist's cache.  Without this measure, when we try to
register two fragments identified by the same hash,
`org-persist-register' returns a structure with the correct metadata
for both, but with the cached image path field missing for the second
fragment.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur fa73360b42
org-latex-preview: Prioritise near latex previews
* lisp/org-latex-preview.el (org-latex-preview-fragments): Reorder
gathered latex fragments so that they are processed starting at the
window start position.  When previewing a large file, this makes a
significant difference to the perceived speed of the processing.
Remove references to `org-preview-latex-image-directory' from this
function, as it is no longer used to cache images.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 396718f97f
org-latex-preview: Avoid duplicate latex fragments
* lisp/org-latex-preview.el (org-latex-preview-collect-fragments): This
function was collecting duplicate org-elements when handling nested
latex environments (For instance, a matrix env inside an equation env).
Fix by comparing the current element with the previously collected one.
2024-01-30 13:37:11 +08:00
TEC 97e662057d
org-latex-preview: Inhibit fsync when editing SVGs
* lisp/org-latex-preview.el (org-latex-preview--svg-make-fg-currentColor):
The value of fsync appears questionable at best on modern hardware, and
makes little sense for small files where the performance overhead is
significant.  Thus, it makes sense to inhibit fsync when editing the
generated SVGs.
2024-01-30 13:37:11 +08:00
Karthik Chikmagalur 9a1cd8df71
org-latex-preview: Add org-preview-latex-auto-mode
* lisp/org-latex-preview.el (org-latex-preview--make-overlay): Add
overlay hooks that correct for any edits made inside the overlay, and
store a copy of the image specification in the "preview-image" slot.
Also return the created overlay, so it can be used in other functions.
(org-latex-preview--from-overlay-p, org-latex-preview--marker,
org-latex-preview--inhibit): New variables to keep track of overlay
state when using auto-mode.
(org-latex-preview--handle-pre-cursor,
org-latex-preview--handle-post-cursor, org-latex-preview--move-into):
Detect when the cursor is entering or leaving a preview overlay, and
trigger the appropriate action.
(org-latex-preview--open-this-overlay,
org-latex-preview--close-previous-overlay): When cursor has entered/left
a preview overlay, show the text/image as appropriate.
(org-latex-preview--handle-insert): Create a dummy preview overlay when
a new LaTeX fragment is created.
(org-latex-preview--insert-front-handler,
org-preview--insert-behind-handler): Extend preview overlay boundaries
when their content changes.
(org-latex-preview-auto-mode): A new minor mode for automatic
opening/closing of preview overlays, and regeneration.
(org-latex-preview-auto-generate-p): Variable which affects the
regeneration behaviour of `org-latex-preview-auto-mode'.
2024-01-30 13:37:03 +08:00
1 changed files with 84 additions and 76 deletions

View File

@ -34,6 +34,7 @@
(declare-function org-persist-read "org-persist")
(declare-function org-persist-register "org-persist")
(declare-function org-persist-unregister "org-persist")
(declare-function eldoc--invoke-strategy "eldoc")
(defvar org-src-mode-hook nil)
(defvar org-src--beg-marker nil)
@ -540,12 +541,12 @@ overlay face is set to `org-latex-preview-processing-face'."
;; This is a temporary measure until a more sophisticated
;; interface for errors is available in Org.
(when errors
(overlay-put
ov 'help-echo
(if (bound-and-true-p tooltip-mode)
errors
(concat (propertize "! " 'face '(bold error))
(substring (replace-regexp-in-string "[\n\r\t ]+" " " errors) 2)))))
(overlay-put
ov 'help-echo
(if (bound-and-true-p tooltip-mode)
errors
(concat (propertize "! " 'face '(bold error))
(substring (replace-regexp-in-string "[\n\r\t ]+" " " errors) 2)))))
(unless (overlay-get ov 'view-text) ;Live previewing this element, update in background
(when image-display (overlay-put ov 'display image-display))
(overlay-put ov 'face (overlay-get ov 'hidden-face)))
@ -894,6 +895,7 @@ customize the variable `org-latex-preview-live'."
(progn
(setq org-latex-preview-auto--marker (make-marker))
(add-hook 'pre-command-hook #'org-latex-preview-auto--handle-pre-cursor nil 'local)
(org-latex-preview-auto--handle-pre-cursor) ; Invoke setup before the hook even fires.
(add-hook 'post-command-hook #'org-latex-preview-auto--handle-post-cursor nil 'local)
(add-hook 'after-change-functions #'org-latex-preview-auto--detect-fragments-in-change nil 'local)
(when org-latex-preview-live
@ -922,7 +924,7 @@ customize the variable `org-latex-preview-live'."
;; show the existing preview image.
;;
;; - A handler is added to `after-change-functions' to regenerate the
;; preview for the fragment.
;; preview for the fragment when its contents change.
;;
;; - When the preview is regenerated, the `after-string' property of
;; the preview overlay is updated to show the new image. This
@ -1002,27 +1004,38 @@ updated no more than once in this interval of time.
Its value is updated dynamically based on the average fragment
preview time.")
(defvar-local org-latex-preview--live-preview-times
(defvar-local org-latex-preview-live--preview-times
(make-vector 3 1.0)
"Vector containing the last three preview run times in this buffer")
(defvar-local org-latex-preview--live-preview-times-index 0)
(defvar-local org-latex-preview-live--preview-times-index 0)
(defun org-latex-preview--live-update-times (latest-duration)
(defun org-latex-preview-live--update-times (latest-duration)
"Update preview times given the last preview took LATEST-DURATION seconds."
(aset org-latex-preview--live-preview-times
(% (cl-incf org-latex-preview--live-preview-times-index) 3)
(aset org-latex-preview-live--preview-times
(% (cl-incf org-latex-preview-live--preview-times-index) 3)
latest-duration)
(setq-local org-latex-preview-live-throttle
(/ (apply #'+ (append org-latex-preview--live-preview-times nil))
(/ (apply #'+ (append org-latex-preview-live--preview-times nil))
3)))
(defun org-latex-preview--live-record-hook (exit-code _buf extended-info)
(defun org-latex-preview-live--record-hook (exit-code _buf extended-info)
"A hook for `org-latex-preview-process-finish-functions' to track preview time.
Called with EXIT-CODE and EXTENDED-INFO from the async process."
(when (= exit-code 0)
(org-latex-preview--live-update-times
(org-latex-preview-live--update-times
(- (float-time) (plist-get extended-info :start-time)))))
(defconst org-latex-preview-live-display-type 'buffer
"How to display live-updating previews of LaTeX snippets.
This option is meaningful when live previews are enabled, by
setting `org-latex-preview-auto-generate' to `live' and enabling
`org-latex-preview-auto-mode'.
The only currently supported option is the symbol buffer, to
display live previews next to or under the LaTeX fragment in the
Org buffer.")
(defun org-latex-preview-live--debounce (func duration)
"Return a debounced FUNC with DURATION applied."
(let ((debounce-timer))
@ -1122,30 +1135,6 @@ BOX-FACE is the face to apply in addition."
(overlay-put ov 'after-string org-latex-preview-live--docstring)))
(org-latex-preview-live--update-props image '(:box t))))))
;; TODO: Add this to the live preview mode setup?
;;
;; Currently not used. Adding this function to
;; `org-latex-preview-process-finish-functions' is potentially more
;; efficient than adding a function to
;; `org-latex-preview-overlay-update-functions' on LaTeX runs with
;; many fragments, but for some reason the image display fails to
;; update until a full redisplay/the user runs a command. This needs
;; to be investigated.
(defun org-latex-preview-live--update-overlay-run (exit-code _buf extended-info)
"TODO."
(when-let* (((= exit-code 0))
(ov
(thread-first
(plist-get extended-info :fragments)
(car)
(plist-get :overlay)))
((overlay-get ov 'after-string)))
(when (memq ov (overlays-at (point)))
(org-latex-preview-live--update-props
(overlay-get ov 'preview-image)))))
;; TODO: Replace with `org-latex-preview-live--update-overlay-run',
;; which see.
(defun org-latex-preview-live--update-overlay (ov)
"Update the live LaTeX preview for overlay OV."
(when (and (memq ov (overlays-at (point)))
@ -1180,7 +1169,7 @@ This is meant to be called via `org-src-mode-hook'."
(let* ((org-buf (marker-buffer org-src--beg-marker))
(src-buf (current-buffer))
(org-buf-visible-p (window-live-p (get-buffer-window org-buf)))
;; Do not use (org-element-property :begin element) to
;; Do not use (org-element-property :begin element) to
;; find the bounds -- this is fragile under typos.
(beg (save-excursion (goto-char (point-min))
(skip-chars-forward "\n \t\r")
@ -1190,6 +1179,9 @@ This is meant to be called via `org-src-mode-hook'."
(point)))
preamble element skip-env-p numbering-offsets ov orig-ov)
(setq org-latex-preview-auto--marker (point-marker))
;; Copy the LaTeX preview overlay from the source Org buffer
;; into the org-src buffer and show a preview image over the
;; former:
(with-current-buffer org-buf
(setq element (org-element-context))
(when (eq (org-element-type element) 'latex-environment)
@ -1215,7 +1207,7 @@ This is meant to be called via `org-src-mode-hook'."
(org-latex-preview-auto--close-previous-overlay))
(unless skip-env-p
(or ov (setq ov (org-latex-preview--ensure-overlay beg end)))
;; Adjust numbering
;; Adjust numbering if required
(when (and org-latex-preview-numbered
(eq (org-element-type element) 'latex-environment))
(with-current-buffer org-buf
@ -1224,7 +1216,8 @@ This is meant to be called via `org-src-mode-hook'."
(when (buffer-local-value 'org-latex-preview-live org-buf)
(if org-buf-visible-p
;; Display live previews in original org buffer
;; Source Org buffer is visible: display live previews
;; over the fragment there
(progn
(setq-local org-latex-preview-live--generator
(thread-first
@ -1244,8 +1237,18 @@ This is meant to be called via `org-src-mode-hook'."
org-latex-preview-live-debounce)))
(add-hook 'after-change-functions org-latex-preview-live--generator 90 'local))
;; Or display live previews in org-src buffer
;; Source Org buffer not visible: display live previews in org-src buffer
;; Set the element type ahead of time since we cannot call
;; org-element-context in the org-src buffer
(setq org-latex-preview-live--element-type
(with-current-buffer org-buf
(or (and (string-prefix-p
"\\[" (org-element-property :value element))
'latex-environment)
(org-element-type element))))
;; Show live preview if available
(org-latex-preview-live--ensure-overlay ov)
;; Set up hooks for live preview updates in the org-src buffer
(add-hook 'org-latex-preview-overlay-update-functions
#'org-latex-preview-live--update-overlay
nil 'local)
@ -1264,10 +1267,10 @@ This is meant to be called via `org-src-mode-hook'."
(org-latex-preview-live--throttle)
(org-latex-preview-live--debounce
org-latex-preview-live-debounce)))
(add-hook 'org-latex-preview-open-functions #'org-latex-preview-live--ensure-overlay nil 'local)
(add-hook 'org-latex-preview-close-functions #'org-latex-preview-live--clearout nil 'local)
(add-hook 'org-latex-preview-overlay-open-functions #'org-latex-preview-live--ensure-overlay nil 'local)
(add-hook 'org-latex-preview-overlay-close-functions #'org-latex-preview-live--clearout nil 'local)
(add-hook 'after-change-functions org-latex-preview-live--generator 90 'local)))
;; auto-mode in the src buffer
;; Turn on auto-mode behavior in the org-src buffer
(add-hook 'pre-command-hook #'org-latex-preview-auto--handle-pre-cursor nil 'local)
(add-hook 'post-command-hook #'org-latex-preview-auto--handle-post-cursor nil 'local)))))
@ -1303,7 +1306,7 @@ See `org-latex-preview-live' for details."
(add-hook 'org-latex-preview-overlay-close-functions #'org-latex-preview-live--clearout nil 'local)
(add-hook 'org-latex-preview-overlay-open-functions #'org-latex-preview-live--ensure-overlay nil 'local)
(add-hook 'after-change-functions org-latex-preview-live--generator 90 'local)
(add-hook 'org-latex-preview-process-finish-functions #'org-latex-preview--live-record-hook nil 'local)
(add-hook 'org-latex-preview-process-finish-functions #'org-latex-preview-live--record-hook nil 'local)
(add-hook 'org-latex-preview-overlay-update-functions #'org-latex-preview-live--update-overlay nil 'local))
(defun org-latex-preview-live--teardown ()
@ -1321,7 +1324,7 @@ See `org-latex-preview-live' for details."
(remove-hook 'org-latex-preview-overlay-open-functions #'org-latex-preview-live--ensure-overlay 'local)
(remove-hook 'after-change-functions org-latex-preview-live--generator 'local)
(remove-hook 'org-latex-preview-overlay-update-functions #'org-latex-preview-live--update-overlay 'local)
(remove-hook 'org-latex-preview-process-finish-functions #'org-latex-preview--live-record-hook 'local)
(remove-hook 'org-latex-preview-process-finish-functions #'org-latex-preview-live--record-hook 'local)
(setq-local org-latex-preview-live--generator nil))
(defun org-latex-preview-clear-overlays (&optional beg end)
@ -2026,23 +2029,28 @@ previews."
(search-forward ": No such file or directory" nil t))))))
(propertize org-latex-preview--latex-log 'face 'warning)
(propertize org-latex-preview--image-log 'face 'warning)))
(cl-loop for fragment in (plist-get extended-info :fragments)
for path = (plist-get fragment :path)
when (not path)
for ov = (plist-get fragment :overlay)
when ov do
;; ;TODO: Other options here include:
;; ;Fringe marker
;; (overlay-put ov 'before-string
;; (propertize "!" 'display
;; `(left-fringe exclamation-mark
;; warning)))
;; ;Special face
;; (unless (overlay-get ov 'face)
;; (overlay-put ov 'face 'org-latex-preview-processing-face))
;;
;; ;Note: ov has buffer extended-info, no need to set current-buffer
(delete-overlay ov)))
(with-current-buffer (plist-get extended-info :org-buffer)
(cl-loop for fragment in (plist-get extended-info :fragments)
for path = (plist-get fragment :path)
when (not path)
for ov = (plist-get fragment :overlay)
when ov do
;; ;TODO: Other options here include:
;; ;Fringe marker
;; (overlay-put ov 'before-string
;; (propertize "!" 'display
;; `(left-fringe exclamation-mark
;; warning)))
;; ;Special face
;; (unless (overlay-get ov 'face)
;; (overlay-put ov 'face 'org-latex-preview-processing-face))
;;
;; ;Note: ov has buffer extended-info, no need to set current-buffer
(let ((start (overlay-start ov))
(end (overlay-end ov)))
(delete-overlay ov)
(when org-latex-preview-auto-mode
(org-latex-preview--ensure-overlay start end))))))
(defvar-local org-latex-preview--preamble-content nil
"Cache of the LaTeX preamble for snippet preview.")
@ -2378,17 +2386,17 @@ fragments are regenerated."
(plist-get bad-fragment :key))
(if (plist-get extended-info :place-preview-p)
(org-latex-preview--update-overlay
(plist-get bad-fragment :overlay)
(org-latex-preview--cache-image
(plist-get bad-fragment :key)
(plist-get bad-fragment :path)
(org-latex-preview--display-info
extended-info bad-fragment)))
(plist-get bad-fragment :overlay)
(org-latex-preview--cache-image
(plist-get bad-fragment :key)
(plist-get bad-fragment :path)
(org-latex-preview--display-info
extended-info bad-fragment)))
(org-latex-preview--cache-image
(plist-get bad-fragment :key)
(plist-get bad-fragment :path)
(org-latex-preview--display-info
extended-info bad-fragment))))
(plist-get bad-fragment :key)
(plist-get bad-fragment :path)
(org-latex-preview--display-info
extended-info bad-fragment))))
;; Re-generate the remaining fragments.
(org-latex-preview--create-image-async
(plist-get extended-info :processor)
@ -2408,7 +2416,7 @@ fragments are regenerated."
(plist-put info :errors (plist-get fragment-info :errors))
info))
;TODO: Figure out why this factor is needed.
;; TODO: Figure out why this factor is needed.
(defconst org-latex-preview--shameful-magic-tex-scaling-factor
1.01659593
"Extra factor for aligning preview image baselines.