From 594ff7ad7454d42dbab37e213714f9827e1061b1 Mon Sep 17 00:00:00 2001
From: Paul Nelson <ultrono@gmail.com>
Date: Thu, 11 Apr 2024 07:33:21 +0200
Subject: [PATCH] Call custom folding functions with consistent point

* tex-fold.el (TeX-fold-hide-item): Call folding spec with point at
beginning of item.
* doc/auctex.texi (Folding): Mention the new convention.
---
 doc/auctex.texi | 9 +++++----
 tex-fold.el     | 4 +++-
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/doc/auctex.texi b/doc/auctex.texi
index 6e9d33c3..c3a02520 100644
--- a/doc/auctex.texi
+++ b/doc/auctex.texi
@@ -2761,10 +2761,11 @@ respective mandatory argument of a @LaTeX{} macro will be used as the
 placeholder.
 
 If the first element is a function symbol, the function will be called
-with all mandatory arguments of the macro and the result of the function
-call will be used as a replacement for the macro.
-Such functions typically return a string, but may also return the
-symbol @code{abort} to indicate that the macro should not be folded.
+with all mandatory arguments of the macro, and with point positioned at
+the beginning of the macro.  The result of the function call will be used
+as a replacement for the macro.  Such functions typically return a string,
+but may also return the symbol @code{abort} to indicate that the macro
+should not be folded.
 
 The placeholder is made by copying the text from the buffer together with
 its properties, i.e.@: its face as well.  If fontification has not
diff --git a/tex-fold.el b/tex-fold.el
index e593aed5..fadc145a 100644
--- a/tex-fold.el
+++ b/tex-fold.el
@@ -816,7 +816,9 @@ That means, put respective properties onto overlay OV."
                            (setq arg-list (append arg-list (list (car arg)))))
                          (setq n (1+ n)))
                        (or (condition-case nil
-                               (apply spec arg-list)
+                               (save-excursion
+                                 (goto-char ov-start)
+                                 (apply spec arg-list))
                              (error nil))
                            "[Error: No content or function found]")))
                     (t (or (TeX-fold-macro-nth-arg spec ov-start ov-end)
-- 
2.39.3 (Apple Git-145)

