Subject: bug#29756: 25.2; Footnote-mode: recursive footnotes [UPDATED PATCH]

From: Boruch Baum <[email-masked]>
Date: Sun, 24 Dec 2017 18:37:31 -0500

On 2017-12-22 16:19, Eli Zaretskii wrote:
> Could you please rewrite the patch without using advice?  We generally
> avoid using that in Emacs's own sources.

Done, but in an organizationally messy manner. In parallel with this bug
report, I had proposed a separate feature on the dev mailing list, and
that snippet got swallowed into code Stefan Monnier is reviewing.

-- 
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
--- /home/optimum/Downloads/footnote.el--downloaded_from_gnu.orig	2017-12-24 18:12:15.000000000 -0500
+++ /home/optimum/Downloads/footnote.el--downloaded_from_gnu	2017-12-24 18:35:34.199506628 -0500
@@ -747,6 +747,40 @@ footnote area, returns `point-max'."
 	(setq i (1+ i)))
       rc)))
 
+(defun Footnote--get-area-point-min (&optional arg)
+  "Return POINT of the beginning of the footnote area, being the
+start of the first footnote. If there is no footnote area,
+returns POINT-MAX. With optional arg, consider the
+footnote-section-tag to be part of the footnote area."
+  (if (not footnote-text-marker-alist)
+    (point-max)
+   (if (not arg)
+     (cdr (first footnote-text-marker-alist))
+    (when arg
+      (if (string-equal footnote-section-tag "")
+        (cdr (first footnote-text-marker-alist))
+       (let ((p (point)) min)
+        (goto-char (cdr (first footnote-text-marker-alist)))
+        (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
+          (setq min (match-beginning 0))
+         ; This `else' should never happen, and indicates an error, ie. footnotes
+         ; already exist and a footnote-section-tag is defined, but the section tag
+         ; hasn't been found. We choose to assume that the user deleted it
+         ; intentionally and wants us to behave in this buffer as if the section tag
+         ; was set "", so we do that, now.
+         (setq footnote-section-tag "")
+         ; HOWEVER: The rest of footnote mode does not currently honor or account
+         ; for this.
+         ;
+         ; To illustrate the difference in behavior, create a few footnotes, delete
+         ; the section tag, and create another footnote. Then undo, comment the
+         ; above line (that sets the tag to ""), re-evaluate this function, and repeat.
+         ;
+         ; TODO: integrate sanity checks at reasonable operational points.
+         (setq min (cdr (first footnote-text-marker-alist))))
+        (goto-char p)
+        min))))))
+
 (defun Footnote-add-footnote (&optional arg)
   "Add a numbered footnote.
 The number the footnote receives is dependent upon the relative location
@@ -755,6 +789,8 @@ If the variable `footnote-narrow-to-foot
 the buffer is narrowed to the footnote body.  The restriction is removed
 by using `Footnote-back-to-message'."
   (interactive "*P")
+  (if (> (point) (Footnote--get-area-point-min "including section tag"))
+    (error "Add footnotes only while in text body."))
   (let ((num
          (if footnote-text-marker-alist
              (if (< (point) (cl-cadar (last footnote-pointer-marker-alist)))