From 2cd1b8cc45ecbc8a62f71138c95b6675781279c2 Mon Sep 17 00:00:00 2001
From: Paul Nelson <ultrono@gmail.com>
Date: Wed, 30 Oct 2024 16:32:33 +0100
Subject: [PATCH] Add folding support for TeX dashes

* tex-fold.el (TeX-fold-em-dash)
(TeX-fold-en-dash): New user options.
(TeX-fold-dashes): New function.
(TeX-fold-region-functions): Add TeX-fold-dashes.

* doc/auctex.texi (Folding Macros and Environments): Document
the new user options.  Add dashes to list of default folding
functions.
---
 doc/auctex.texi | 10 +++++++++-
 tex-fold.el     | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/doc/auctex.texi b/doc/auctex.texi
index 718d4c2a..deb073a5 100644
--- a/doc/auctex.texi
+++ b/doc/auctex.texi
@@ -2652,7 +2652,7 @@ around the mark will be kept unfolded.
 This variable is a list of functions which allow the user, or external
 packages, to fold additional @LaTeX{} constructs beyond those supported by
 default.  By default, it is configured to support folding for verbatim
-environments and quotes.
+environments, quotes and dashes.
 @end defopt
 
 @deffn Command TeX-fold-region
@@ -2983,6 +2983,14 @@ inserted.  This option is consulted by @code{TeX-insert-quote} when
 determining whether to fold newly inserted quotes.
 @end defopt
 
+@defopt TeX-fold-em-dash
+Display string used when folding em-dashes ("---") outside math mode and verbatim contexts.  The default value is a Unicode em-dash.
+@end defopt
+
+@defopt TeX-fold-en-dash
+Display string used when folding en-dashes ("--") outside math mode and verbatim contexts.  The default value is a Unicode en-dash.
+@end defopt
+
 @node Outline
 @section Outlining the Document
 @cindex Outlining
diff --git a/tex-fold.el b/tex-fold.el
index 2c10cc27..ec478259 100644
--- a/tex-fold.el
+++ b/tex-fold.el
@@ -363,7 +363,7 @@ and `TeX-fold-math-spec-list', and environments in `TeX-fold-env-spec-list'."
       (TeX-fold-clearout-region start end)
       (TeX-fold-region start end))))
 
-(defcustom TeX-fold-region-functions '(TeX-fold-verbs TeX-fold-quotes)
+(defcustom TeX-fold-region-functions '(TeX-fold-verbs TeX-fold-quotes TeX-fold-dashes)
   "List of additional functions to call when folding a region.
 Each function is called with two arguments, the start and end positions
 of the region to fold."
@@ -1441,6 +1441,40 @@ verbatim contexts and comments."
                         (texmathp))
               (TeX-fold-make-overlay quote-start quote-end 'misc str str))))))))
 
+;;;; Dashes
+
+(defcustom TeX-fold-em-dash "—"
+  "Folded version of em-dash (---)."
+  :type 'string
+  :group 'TeX-fold)
+
+(defcustom TeX-fold-en-dash "–"
+  "Folded version of en-dash (--)."
+  :type 'string
+  :group 'TeX-fold)
+
+(defun TeX-fold-dashes (start end)
+  "Fold LaTeX dashes between START and END.
+Replaces --- with em-dash and -- with en-dash, except in math
+environments, verbatim contexts and comments."
+  (save-excursion
+    (goto-char start)
+    (while (re-search-forward "\\b\\(---\\|--\\)\\b" end t)
+      (let ((dash-start (match-beginning 0))
+            (dash-end (match-end 0))
+            (dash-type (match-string 0)))
+        (unless (or (TeX-in-commented-line)
+                    (and (fboundp 'font-latex-faces-present-p)
+                         (font-latex-faces-present-p
+                          '(font-latex-verbatim-face
+                            font-latex-math-face)
+                          dash-start))
+                    (texmathp))
+          (let ((str (if (string= dash-type "---")
+                         TeX-fold-em-dash
+                       TeX-fold-en-dash)))
+            (TeX-fold-make-overlay dash-start dash-end 'misc str str)))))))
+
 (provide 'tex-fold)
 
 ;;; tex-fold.el ends here
-- 
2.39.3 (Apple Git-145)

