Subject: bug#73302: TeX-find-closing-brace and verbatim macros

From: Paul Nelson <[email-masked]>
Date: Mon, 30 Sep 2024 20:06:15 +0100

Hi Arash, thanks, the modified code you supplied (after a couple
tweaks) addresses all examples considered thus far, and indeed feels
less brittle.  Pushed to my package repo, copying for below in case of
curiosity.

(defun czm-tex-fold--verb-data (&rest _args)
  "Return data for a verbatim macro.
Returns a list of the form (START END CONTENT)."
  (when-let* ((boundaries (LaTeX-verbatim-macro-boundaries))
              (bound-start (car boundaries))
              (bound-end (cdr boundaries))
              (end-delim-char (char-before bound-end))
              (start-delim-char (if (= end-delim-char ?\})
                                    ?\{
                                  end-delim-char))
              (start-delim (char-to-string start-delim-char))
              (verb-arg-start
               (1+ (progn
                     (goto-char bound-end)
                     (if (string= start-delim TeX-grop)
                         (progn (backward-sexp) (point))
                       (forward-char -1)
                       (search-backward start-delim bound-start t)))))
              (verb-arg-end (1- bound-end)))
    (list bound-start
          bound-end
          (buffer-substring-no-properties verb-arg-start
                                          verb-arg-end))))

(defun czm-tex-fold-verbs (start end)
  "Fold verbatim macros between START and END."
  (save-excursion
    (goto-char start)
    (let ((re (concat "\\\\" (regexp-opt
                              (append
                               (LaTeX-verbatim-macros-with-braces)
                               (LaTeX-verbatim-macros-with-delims))))))
      (while (let ((case-fold-search nil))
               (re-search-forward re end t))
        (when-let* ((data (czm-tex-fold--verb-data))
                    (spec (lambda (&rest _args)
                            (nth 2 (czm-tex-fold--verb-data)))))
          (apply #'czm-tex-fold--create-misc-overlay (append data
(list spec))))))))