emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>, Tony Zorman <mail@tony-zorman.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Using org-latex-preview in other major modes
Date: Mon, 07 Oct 2024 17:40:59 -0700	[thread overview]
Message-ID: <87a5fffmuc.fsf@gmail.com> (raw)
In-Reply-To: <87msq5o03b.fsf@localhost>

>> I'm interested in adjusting org-latex-preview to work in other major
>> modes (most notably LaTeX-mode itself, so one can get rid of preview.el).
>> On the advice of Karthik (Cc'd) I'll move the discussion from private
>> mails to this list, so more people who might be interested can join in.
>> ...
>> [1]: https://abode.karthinks.com/org-latex-preview/latex-preview-everywhere.html
>
> Abstracting away previews is certainly welcome.
> RMS explicitly asked Org mode team to work towards this goal:
> https://list.orgmode.org/E1kIkxv-0007iy-Av@fencepost.gnu.org/

I recently spent some time on this feature (LaTeX previews in all
major-modes) and produced a few prototypes.  You can see some demos in

https://www.youtube.com/watch?v=u44X_th6_oY [1]

The way support for other major modes works right now is based on the
following observation: The `org-latex-preview' feature uses a tiny
subset of the `org-element' API.  Specifically, it uses only the
functions

- `org-element-context'
- `org-element-property', referring to the properties :value,
  :begin and :end, and optionally :post-blank and :post-affiliated
- `org-element-type', which returns either latex-fragment,
  latex-environment or any other symbol

In addition, it uses two functions to generate the preamble and find
section bounds:

- `org-latex-preview--get-preamble'
- `org-latex-preview--section-bounds'

If a major-mode can provide drop-in replacements for all these
functions, we can run org-latex-preview -- all of it, including live
previews etc, in that mode.[2]

I can share the full code in a dev branch soon after cleaning it up.  In
this email, I was hoping to solicit some feedback on the Emacs-wide API
for this, which I describe below:

----------------------

I made calls to the above `org-elment-*' functions "pluggable", by
storing these functions in buffer-local variables named

- `org-latex-preview--fcontext'
- `org-latex-preview--fproperty'
- `org-latex-preview--ftype'

and so on.  Instead of calling `org-element-*' like this:

(org-element-property :value (org-element-context))

org-latex-preview now uses

(funcall org-latex-preview--fproperty
         :value
         (funcall org-latex-preview--fcontext))

A major mode can register these handlers with org-latex-preview like
this (example for previews in LaTeX-mode, adapted from Tony's code):

(setf (alist-get 'LaTeX-mode org-latex-preview-interfaces)
      `(:preamble ,#'latex-latex-preview--make-preamble
        :section  ,#'latex-latex-preview-section-bounds
        :context  ,#'latex-latex-preview-context
        :type     ,#'latex-latex-preview-type
        :property ,#'latex-latex-preview-property))
        
See [1] for the definition of these `latex-latex-*' functions.

These functions in `org-latex-preview-interfaces' are set as the values
of the various `org-latex-preview--f*' variables during the first call
to org-latex-preview in the buffer. There is no continuous runtime
dispatch based on the major-mode.  (Since we run many of these functions
in the `after-change-functions' hook, we really want to avoid runtime
dispatch.)

Is this the best way to go about this?

Karthik

[1]: To see what the "adapter" code looks like for different modes, here
are some examples:
https://paste.karthinks.com/ac64169c-prog-latex-preview.el.html
https://paste.karthinks.com/6ba26238-calc-latex-preview.el.html
https://paste.karthinks.com/44930b4e-latex-latex-preview.el.html

[2]: This is without handling accurate numbering.  Numbering requires a
couple more replacement functions that I can describe if required.


  parent reply	other threads:[~2024-10-08  0:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-07  7:22 Using org-latex-preview in other major modes Tony Zorman
2024-04-07 11:59 ` Ihor Radchenko
2024-04-07 18:06   ` Karthik Chikmagalur
2024-04-09 14:11     ` Ihor Radchenko
2024-10-08  0:40   ` Karthik Chikmagalur [this message]
2024-10-10 17:35     ` Ihor Radchenko
2024-04-07 17:48 ` Karthik Chikmagalur
2024-04-08  6:23   ` Tony Zorman
2024-04-08  6:36     ` Karthik Chikmagalur
2024-04-09 20:06       ` Tony Zorman
2024-04-21 19:10         ` Tony Zorman
2024-04-22  3:41           ` Karthik Chikmagalur
2024-04-22  9:29             ` Tony Zorman

Reply instructions:

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

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

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

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

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

  git send-email \
    --in-reply-to=87a5fffmuc.fsf@gmail.com \
    --to=karthikchikmagalur@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@tony-zorman.com \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

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

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

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

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