auctex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tex-fold.el: allow macro folding specifications to "abort"


From: Paul Nelson
Subject: tex-fold.el: allow macro folding specifications to "abort"
Date: Mon, 28 Aug 2023 08:52:52 +0200

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.

For example, with the suggested modification, the following configuration would 
allow the user to fold \begin{...} and \end{...} environments (as "↴" and "↲") 
except when those environments belong to a specified list of exceptions, in 
which case they would not be folded at all:

#+begin_src elisp
(defvar my-fold-exclude-list
  '("equation" "equation*" "align" "align*" "multline" "multline*"))

(defun my-fold-helper (type env &rest args)
  (if (member env my-fold-exclude-list)
      'abort
    (if (eq type 'begin) "↴" "↲")))

(defun my-fold-begin (env &rest args)
  (my-fold-helper 'begin env args))

(defun my-fold-end (env &rest args)
  (my-fold-helper 'end env args))

(add-to-list 'TeX-fold-macro-spec-list '(my-fold-begin ("begin")))
(add-to-list 'TeX-fold-macro-spec-list '(my-fold-end ("end")))
#+end_src

The package https://github.com/ultronozm/czm-tex-fold.el contains other 
examples (implemented via override advice to ~TeX-fold-hide-item~, which I 
would be able to avoid via the proposed patch).

Here's the proposed modification (almost all of which is indentation):

#+begin_example
diff --git a/tex-fold.el b/tex-fold.el
index 78b58d4..516bbff 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,36 @@ 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)
+        (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.
#+end_example




reply via email to

[Prev in Thread] Current Thread [Next in Thread]