Hi Paul,

>>>>> Paul Nelson <ultr...@gmail.com> writes:
> I'd like to suggest a modification to tex-fold.el: when the macro
> specification is a function, allow it to return the elisp symbol
> ~abort~ (rather than a string) to signal that macro folding should be
> aborted. This would to allow the user to decide programmatically not
> to fold certain macros. It requires a "one-line" change to
> ~TeX-fold-hide-item~ (see patch below) with no effect on existing
> usage.

Thanks for your suggestion. It seems good to me. I'd push the attached
commit unless someone else objects.

(I presume this change doesn't need copyright assignment form because
most of it is indent adaptation and the substantial change is just
addition of
----------------------------------------------------------------------
    (if (eq computed 'abort)
        (progn (delete-overlay ov)
               t ; so that `TeX-fold-dwim' "gives up"
               )
----------------------------------------------------------------------
. But this may be a border case. Is there anyone who think it needs
copyright assignment?)

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine

>From 9ba1612c357591aea7f479cc976c525dbca77c46 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <ik...@ikumi.que.jp>
Date: Wed, 30 Aug 2023 15:56:53 +0900
Subject: [PATCH] Allow programmatical folding

* tex-fold.el (TeX-fold-hide-item): Abort folding if computed result
is symbol `abort'.
(TeX-fold-macro-spec-list): Mention the new feature.
* doc/auctex.texi (Folding): Mention the new feature.
---
 doc/auctex.texi |  2 ++
 tex-fold.el     | 61 +++++++++++++++++++++++++++++--------------------
 2 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/doc/auctex.texi b/doc/auctex.texi
index 61f12577..087d1405 100644
--- a/doc/auctex.texi
+++ b/doc/auctex.texi
@@ -2747,6 +2747,8 @@ 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.
 
 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 78b58d4d..50e9e41b 100644
--- a/tex-fold.el
+++ b/tex-fold.el
@@ -111,6 +111,8 @@ the respective macro argument.
 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 `abort' to indicate that the macro should not be folded.
 
 Setting this variable does not take effect immediately.  Use
 Customize or reset the mode."
@@ -796,31 +798,40 @@ That means, put respective properties onto overlay OV."
          (display-string (if (listp computed) (car computed) computed))
          ;; (face (when (listp computed) (cadr computed)))
          )
-    ;; Do nothing if the overlay is empty.
-    (when (and ov-start ov-end)
-      ;; Cater for zero-length display strings.
-      (when (string= display-string "") (setq display-string TeX-fold-ellipsis))
-      ;; Add a linebreak to the display string and adjust the overlay end
-      ;; in case of an overfull line.
-      (when (TeX-fold-overfull-p ov-start ov-end display-string)
-        (setq display-string (concat display-string "\n"))
-        (move-overlay ov ov-start (save-excursion
-                                    (goto-char ov-end)
-                                    (skip-chars-forward " \t")
-                                    (point))))
-      (overlay-put ov 'mouse-face 'highlight)
-      (when font-lock-mode
-        ;; Add raise adjustment for superscript and subscript.
-        ;; (bug#42209)
-        (setq display-string
-              (propertize display-string
-                          'display (get-text-property ov-start 'display))))
-      (overlay-put ov 'display display-string)
-      (when font-lock-mode
-        (overlay-put ov 'face TeX-fold-folded-face))
-      (unless (zerop TeX-fold-help-echo-max-length)
-        (overlay-put ov 'help-echo (TeX-fold-make-help-echo
-                                    (overlay-start ov) (overlay-end ov)))))))
+
+    (if (eq computed 'abort)
+        ;; Do nothing if computed result is symbol `abort' to allow
+        ;; programmatical customization.
+        ;; Suggested by Paul Nelson <ultr...@gmail.com>.
+        ;; <URL:https://lists.gnu.org/r/auctex/2023-08/msg00026.html>
+        (progn (delete-overlay ov)
+               t ; so that `TeX-fold-dwim' "gives up"
+               )
+      ;; Do nothing if the overlay is empty.
+      (when (and ov-start ov-end)
+        ;; Cater for zero-length display strings.
+        (when (string= display-string "") (setq display-string TeX-fold-ellipsis))
+        ;; Add a linebreak to the display string and adjust the overlay end
+        ;; in case of an overfull line.
+        (when (TeX-fold-overfull-p ov-start ov-end display-string)
+          (setq display-string (concat display-string "\n"))
+          (move-overlay ov ov-start (save-excursion
+                                      (goto-char ov-end)
+                                      (skip-chars-forward " \t")
+                                      (point))))
+        (overlay-put ov 'mouse-face 'highlight)
+        (when font-lock-mode
+          ;; Add raise adjustment for superscript and subscript.
+          ;; (bug#42209)
+          (setq display-string
+                (propertize display-string
+                            'display (get-text-property ov-start 'display))))
+        (overlay-put ov 'display display-string)
+        (when font-lock-mode
+          (overlay-put ov 'face TeX-fold-folded-face))
+        (unless (zerop TeX-fold-help-echo-max-length)
+          (overlay-put ov 'help-echo (TeX-fold-make-help-echo
+                                      (overlay-start ov) (overlay-end ov))))))))
 
 (defun TeX-fold-show-item (ov)
   "Show a single LaTeX macro or environment.
-- 
2.41.0

Reply via email to