Subject: bug#70295: [PATCH] Allow preprocessing of previews

From: Arash Esbati <[email-masked]>
Date: Tue, 09 Apr 2024 11:17:22 +0200

Hi Paul,

Paul Nelson <[email-masked]> writes:

> The attached patch introduces an optional preprocessing step to
> preview-region.
>
> As motivation, I've been using such functionality (implemented via
> advice) in https://github.com/ultronozm/czm-preview.el to give correct
> equation numbers in previews.  This patch would allow me to excise the
> advice.

Thanks, the change is non-intrusive enough so I don't see why we should
not install it.

> From 417c9e465b4d0cd507e122268c8caad8d74d76f3 Mon Sep 17 00:00:00 2001
> From: Paul Nelson <[email-masked]>
> Date: Tue, 9 Apr 2024 05:04:56 +0200
> Subject: [PATCH] Allow preprocessing of previews
>
> * preview.el.in (preview--preprocess-function): New variable.
> (preview-region): Use the new variable (if non-nil) to
> preprocess the region being previewed.
> ---
>  preview.el.in | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/preview.el.in b/preview.el.in
> index 95410439..6afff5b9 100644
> --- a/preview.el.in
> +++ b/preview.el.in
> @@ -4030,6 +4030,9 @@ stored in `preview-dumped-alist'."
>    (preview-format-kill old-format)
>    (setcdr old-format nil))
>  
> +(defvar preview--preprocess-function nil
> +  "Function used to preprocess region before previewing.")

This variable will be set by your package, right?  Then it doesn't make
sense to me to declare it as an internal one.  What do you think about
something like this:

(defvar preview-preprocess-function nil
  "Function used to preprocess region before previewing.
The function bound to this variable will be called inside
`preview-region' with one argument which is a string.")

>  (defun preview-region (begin end)
>    "Run preview on region between BEGIN and END."
>    (interactive "r")
> @@ -4038,7 +4041,10 @@ stored in `preview-dumped-alist'."
>           (concat (preview--counter-information begin)
>                   TeX-region-extra)))
>      (TeX-region-create (TeX-region-file TeX-default-extension)
> -                       (buffer-substring-no-properties begin end)
> +                       (let ((str (buffer-substring-no-properties begin end)))
> +                         (if preview--preprocess-function
> +                             (funcall preview--preprocess-function str)

Need to be adjusted then.

> +                           str))
>                         (if buffer-file-name
>                             (file-name-nondirectory buffer-file-name)
>                           "<none>")

WDYT?  Best, Arash