]> code.delx.au - gnu-emacs/blobdiff - lisp/mail/footnote.el
Update copyright year to 2015
[gnu-emacs] / lisp / mail / footnote.el
index b5ec6f02260ec452351b4ef566d9a7e0d142d0d0..ea674434a2fbfc5bfcd05fd205d1fcdc9f357f12 100644 (file)
@@ -1,6 +1,6 @@
-;;; footnote.el --- footnote support for message mode  -*- coding: iso-latin-1;-*-
+;;; footnote.el --- footnote support for message mode  -*- coding: utf-8;-*-
 
-;; Copyright (C) 1997, 2000 by Free Software Foundation, Inc.
+;; Copyright (C) 1997, 2000-2015 Free Software Foundation, Inc.
 
 ;; Author: Steven L Baur <steve@xemacs.org>
 ;; Keywords: mail, news
@@ -8,20 +8,18 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify it
-;; under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
-;; GNU Emacs is distributed in the hope that it will be useful, but
-;; WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
-;; General Public License for more details.
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the Free
-;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-;; 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -37,9 +35,8 @@
 
 ;;; Code:
 
-(eval-when-compile
-  (require 'cl)
-  (defvar filladapt-token-table))
+(eval-when-compile (require 'cl-lib))
+(defvar filladapt-token-table)
 
 (defgroup footnote nil
   "Support for footnotes in mail and news messages."
   :group 'message)
 
 (defcustom footnote-mode-line-string " FN"
-  "*String to display in modes section of the mode-line."
+  "String to display in modes section of the mode-line."
+  :type 'string
   :group 'footnote)
 
 (defcustom footnote-mode-hook nil
-  "*Hook functions run when footnote-mode is activated."
+  "Hook functions run when footnote-mode is activated."
   :type 'hook
   :group 'footnote)
 
 (defcustom footnote-narrow-to-footnotes-when-editing nil
-  "*If set, narrow to footnote text body while editing a footnote."
+  "If non-nil, narrow to footnote text body while editing a footnote."
   :type 'boolean
   :group 'footnote)
 
 (defcustom footnote-prompt-before-deletion t
-  "*If set, prompt before deleting a footnote.
+  "If non-nil, prompt before deleting a footnote.
 There is currently no way to undo deletions."
   :type 'boolean
   :group 'footnote)
 
 (defcustom footnote-spaced-footnotes t
-  "If set true it will put a blank line between each footnote.
-If nil, no blank line will be inserted."
+  "If non-nil, insert an empty line between footnotes.
+Customizing this variable has no effect on buffers already
+displaying footnotes."
   :type 'boolean
   :group 'footnote)
 
-(defcustom footnote-use-message-mode t
-  "*If non-nil assume Footnoting will be done in message-mode."
+(defcustom footnote-use-message-mode t ; Nowhere used.
+  "If non-nil, assume Footnoting will be done in `message-mode'."
   :type 'boolean
   :group 'footnote)
 
 (defcustom footnote-body-tag-spacing 2
-  "*Number of blanks separating a footnote body tag and its text."
+  "Number of spaces separating a footnote body tag and its text.
+Customizing this variable has no effect on buffers already
+displaying footnotes."
   :type 'integer
   :group 'footnote)
 
-(defvar footnote-prefix [(control ?c) ?!]
-  "*When not using message mode, the prefix to bind in `mode-specific-map'")
+(defcustom footnote-prefix [(control ?c) ?!]
+  "Prefix key to use for Footnote command in Footnote minor mode.
+The value of this variable is checked as part of loading Footnote mode.
+After that, changing the prefix key requires manipulating keymaps."
+  :type 'key-sequence
+  :group 'footnote)
 
 ;;; Interface variables that probably shouldn't be changed
 
 (defcustom footnote-section-tag "Footnotes: "
-  "*Tag inserted at beginning of footnote section."
-  :version "21.4"
+  "Tag inserted at beginning of footnote section.
+If you set this to the empty string, no tag is inserted and the
+value of `footnote-section-tag-regexp' is ignored.  Customizing
+this variable has no effect on buffers already displaying
+footnotes."
   :type 'string
   :group 'footnote)
 
 (defcustom footnote-section-tag-regexp "Footnotes\\(\\[.\\]\\)?: "
-  "*Regexp which indicates the start of a footnote section.
-See also `footnote-section-tag'."
+  "Regexp which indicates the start of a footnote section.
+This variable is disregarded when `footnote-section-tag' is the
+empty string.  Customizing this variable has no effect on buffers
+already displaying footnotes."
   :type 'regexp
   :group 'footnote)
 
 ;; The following three should be consumed by footnote styles.
 (defcustom footnote-start-tag "["
-  "*String used to denote start of numbered footnote."
+  "String used to denote start of numbered footnote.
+Should not be set to the empty string.  Customizing this variable
+has no effect on buffers already displaying footnotes."
   :type 'string
   :group 'footnote)
 
 (defcustom footnote-end-tag "]"
-  "*String used to denote end of numbered footnote."
+  "String used to denote end of numbered footnote.
+Should not be set to the empty string.  Customizing this variable
+has no effect on buffers already displaying footnotes."
   :type 'string
   :group 'footnote)
 
-(defvar footnote-signature-separator (if (boundp 'message-signature-separator)
-                                        message-signature-separator
-                                      "^-- $")
-  "*String used to recognize .signatures.")
+(defcustom footnote-signature-separator (if (boundp 'message-signature-separator)
+                                           message-signature-separator
+                                         "^-- $")
+  "Regexp used by Footnote mode to recognize signatures."
+  :type 'regexp
+  :group 'footnote)
 
 ;;; Private variables
 
@@ -132,13 +148,9 @@ See also `footnote-section-tag'."
 (defvar footnote-mouse-highlight 'highlight
   "Text property name to enable mouse over highlight.")
 
-(defvar footnote-mode nil
-  "Variable indicating whether footnote minor mode is active.")
-(make-variable-buffer-local 'footnote-mode)
-
 ;;; Default styles
 ;;; NUMERIC
-(defconst footnote-numeric-regexp "[0-9]"
+(defconst footnote-numeric-regexp "[0-9]+"
   "Regexp for digits.")
 
 (defun Footnote-numeric (n)
@@ -150,7 +162,7 @@ Use Arabic numerals for footnoting."
 (defconst footnote-english-upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   "Upper case English alphabet.")
 
-(defconst footnote-english-upper-regexp "[A-Z]"
+(defconst footnote-english-upper-regexp "[A-Z]+"
   "Regexp for upper case English alphabet.")
 
 (defun Footnote-english-upper (n)
@@ -169,7 +181,7 @@ Wrapping around the alphabet implies successive repetitions of letters."
 (defconst footnote-english-lower "abcdefghijklmnopqrstuvwxyz"
   "Lower case English alphabet.")
 
-(defconst footnote-english-lower-regexp "[a-z]"
+(defconst footnote-english-lower-regexp "[a-z]+"
   "Regexp of lower case English alphabet.")
 
 (defun Footnote-english-lower (n)
@@ -190,7 +202,7 @@ Wrapping around the alphabet implies successive repetitions of letters."
     (50 . "l") (100 . "c") (500 . "d") (1000 . "m"))
   "List of roman numerals with their values.")
 
-(defconst footnote-roman-lower-regexp "[ivxlcdm]"
+(defconst footnote-roman-lower-regexp "[ivxlcdm]+"
   "Regexp of roman numerals.")
 
 (defun Footnote-roman-lower (n)
@@ -203,7 +215,7 @@ Wrapping around the alphabet implies successive repetitions of letters."
     (50 . "L") (100 . "C") (500 . "D") (1000 . "M"))
   "List of roman numerals with their values.")
 
-(defconst footnote-roman-upper-regexp "[IVXLCDM]"
+(defconst footnote-roman-upper-regexp "[IVXLCDM]+"
   "Regexp of roman numerals.  Not complete")
 
 (defun Footnote-roman-upper (n)
@@ -266,14 +278,37 @@ Wrapping around the alphabet implies successive repetitions of letters."
 
 ;; Latin-1
 
-(defconst footnote-latin-regexp "¹²³ºª§¶"
+(defconst footnote-latin-string "¹²³ºª§¶"
+  "String of Latin-1 footnoting characters.")
+
+;; Note not [...]+, because this style cycles.
+(defconst footnote-latin-regexp (concat "[" footnote-latin-string "]")
   "Regexp for Latin-1 footnoting characters.")
 
 (defun Footnote-latin (n)
   "Latin-1 footnote style.
 Use a range of Latin-1 non-ASCII characters for footnoting."
-  (string (aref footnote-latin-regexp
-               (mod (1- n) (length footnote-latin-regexp)))))
+  (string (aref footnote-latin-string
+               (mod (1- n) (length footnote-latin-string)))))
+
+;; Unicode
+
+(defconst footnote-unicode-string "⁰¹²³⁴⁵⁶⁷⁸⁹"
+  "String of Unicode footnoting characters.")
+
+(defconst footnote-unicode-regexp (concat "[" footnote-unicode-string "]+")
+  "Regexp for Unicode footnoting characters.")
+
+(defun Footnote-unicode (n)
+  "Unicode footnote style.
+Use Unicode characters for footnoting."
+  (let (modulus result done)
+    (while (not done)
+      (setq modulus (mod n 10)
+            n (truncate n 10))
+      (and (zerop n) (setq done t))
+      (push (aref footnote-unicode-string modulus) result))
+    (apply #'string result)))
 
 ;;; list of all footnote styles
 (defvar footnote-style-alist
@@ -282,7 +317,8 @@ Use a range of Latin-1 non-ASCII characters for footnoting."
     (english-upper Footnote-english-upper ,footnote-english-upper-regexp)
     (roman-lower Footnote-roman-lower ,footnote-roman-lower-regexp)
     (roman-upper Footnote-roman-upper ,footnote-roman-upper-regexp)
-    (latin Footnote-latin ,footnote-latin-regexp))
+    (latin Footnote-latin ,footnote-latin-regexp)
+    (unicode Footnote-unicode ,footnote-unicode-regexp))
   "Styles of footnote tags available.
 By default only boring Arabic numbers, English letters and Roman Numerals
 are available.
@@ -290,21 +326,29 @@ See footnote-han.el, footnote-greek.el and footnote-hebrew.el for more
 exciting styles.")
 
 (defcustom footnote-style 'numeric
-  "*Style used for footnoting.
+  "Default style used for footnoting.
 numeric == 1, 2, 3, ...
 english-lower == a, b, c, ...
 english-upper == A, B, C, ...
 roman-lower == i, ii, iii, iv, v, ...
 roman-upper == I, II, III, IV, V, ...
-latin == ¹ ² ³ º ª § ¶
-See also variables `footnote-start-tag' and `footnote-end-tag'."
+latin == ¹ ² ³ º ª § ¶
+unicode == ¹, ², ³, ...
+See also variables `footnote-start-tag' and `footnote-end-tag'.
+
+Note: some characters in the unicode style may not show up
+properly if the default font does not contain those characters.
+
+Customizing this variable has no effect on buffers already
+displaying footnotes.  To change the style of footnotes in such a
+buffer use the command `Footnote-set-style'."
   :type (cons 'choice (mapcar (lambda (x) (list 'const (car x)))
                              footnote-style-alist))
   :group 'footnote)
 
 ;;; Style utilities & functions
 (defun Footnote-style-p (style)
-  "Return non-nil if style is a valid style known to footnote-mode."
+  "Return non-nil if style is a valid style known to `footnote-mode'."
   (assq style footnote-style-alist))
 
 (defun Footnote-index-to-string (index)
@@ -318,7 +362,8 @@ Conversion is done based upon the current selected style."
 (defun Footnote-current-regexp ()
   "Return the regexp of the index of the current style."
   (concat (nth 2 (or (assq footnote-style footnote-style-alist)
-                    (nth 0 footnote-style-alist))) "*"))
+                    (nth 0 footnote-style-alist)))
+         "*"))
 
 (defun Footnote-refresh-footnotes (&optional index-regexp)
   "Redraw all footnotes.
@@ -333,16 +378,21 @@ styles."
        (setq locn (cdr alist))
        (while locn
          (goto-char (car locn))
-         (search-backward footnote-start-tag nil t)
-         (when (looking-at (concat
-                            (regexp-quote footnote-start-tag)
-                            "\\(" index-regexp "\\)"
-                            (regexp-quote footnote-end-tag)))
-           (replace-match (concat
-                           footnote-start-tag
-                           (Footnote-index-to-string (1+ i))
-                           footnote-end-tag)
-                          nil "\\1"))
+         ;; Try to handle the case where `footnote-start-tag' and
+         ;; `footnote-end-tag' are the same string.
+         (when (looking-back (concat
+                              (regexp-quote footnote-start-tag)
+                              "\\(" index-regexp "+\\)"
+                              (regexp-quote footnote-end-tag))
+                             (line-beginning-position))
+           (replace-match
+            (propertize
+             (concat
+              footnote-start-tag
+              (Footnote-index-to-string (1+ i))
+              footnote-end-tag)
+             'footnote-number (1+ i) footnote-mouse-highlight t)
+            nil "\\1"))
          (setq locn (cdr locn)))
        (setq i (1+ i))))
 
@@ -352,13 +402,16 @@ styles."
        (goto-char (cdr alist))
        (when (looking-at (concat
                           (regexp-quote footnote-start-tag)
-                          "\\(" index-regexp "\\)"
+                          "\\(" index-regexp "+\\)"
                           (regexp-quote footnote-end-tag)))
-         (replace-match (concat
-                         footnote-start-tag
-                         (Footnote-index-to-string (1+ i))
-                         footnote-end-tag)
-                        nil "\\1"))
+         (replace-match
+          (propertize
+           (concat
+            footnote-start-tag
+            (Footnote-index-to-string (1+ i))
+            footnote-end-tag)
+           'footnote-number (1+ i))
+          nil "\\1"))
        (setq i (1+ i))))))
 
 (defun Footnote-assoc-index (key alist)
@@ -389,23 +442,21 @@ styles."
    (list (intern (completing-read
                  "Footnote Style: "
                  obarray #'Footnote-style-p 'require-match))))
-  (setq footnote-style style))
+  (let ((old (Footnote-assoc-index footnote-style footnote-style-alist)))
+    (setq footnote-style style)
+    (Footnote-refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
 
 ;; Internal functions
 (defun Footnote-insert-numbered-footnote (arg &optional mousable)
   "Insert numbered footnote at (point)."
-  (let* ((start (point))
-        (end (progn
-               (insert-before-markers (concat footnote-start-tag
-                                              (Footnote-index-to-string arg)
-                                              footnote-end-tag))
-               (point))))
-
-    (add-text-properties start end
-                        (list 'footnote-number arg))
-    (when mousable
-      (add-text-properties start end
-                          (list footnote-mouse-highlight t)))))
+  (let ((string (concat footnote-start-tag
+                       (Footnote-index-to-string arg)
+                       footnote-end-tag)))
+    (insert-before-markers
+     (if mousable
+        (propertize
+         string 'footnote-number arg footnote-mouse-highlight t)
+       (propertize string 'footnote-number arg)))))
 
 (defun Footnote-renumber (from to pointer-alist text-alist)
   "Renumber a single footnote."
@@ -414,29 +465,27 @@ styles."
     (setcar text-alist to)
     (while posn-list
       (goto-char (car posn-list))
-      (search-backward footnote-start-tag nil t)
-      (when (looking-at (format "%s%s%s"
-                               (regexp-quote footnote-start-tag)
-                               (Footnote-current-regexp)
-                               (regexp-quote footnote-end-tag)))
-       (add-text-properties (match-beginning 0) (match-end 0)
-                            (list 'footnote-number to))
-       (replace-match (format "%s%s%s"
-                              footnote-start-tag
-                              (Footnote-index-to-string to)
-                              footnote-end-tag)))
+      (when (looking-back (concat (regexp-quote footnote-start-tag)
+                                 (Footnote-current-regexp)
+                                 (regexp-quote footnote-end-tag))
+                         (line-beginning-position))
+       (replace-match
+        (propertize
+         (concat footnote-start-tag
+                 (Footnote-index-to-string to)
+                 footnote-end-tag)
+         'footnote-number to footnote-mouse-highlight t)))
       (setq posn-list (cdr posn-list)))
     (goto-char (cdr text-alist))
-    (when (looking-at (format "%s%s%s"
-                             (regexp-quote footnote-start-tag)
+    (when (looking-at (concat (regexp-quote footnote-start-tag)
                              (Footnote-current-regexp)
                              (regexp-quote footnote-end-tag)))
-      (add-text-properties (match-beginning 0) (match-end 0)
-                          (list 'footnote-number to))
-      (replace-match (format "%s%s%s"
-                            footnote-start-tag
-                            (Footnote-index-to-string to)
-                            footnote-end-tag) nil t))))
+      (replace-match
+       (propertize
+       (concat footnote-start-tag
+               (Footnote-index-to-string to)
+               footnote-end-tag)
+       'footnote-number to)))))
 
 ;; Not needed?
 (defun Footnote-narrow-to-footnotes ()
@@ -445,8 +494,13 @@ styles."
   (goto-char (point-max))
   (when (re-search-backward footnote-signature-separator nil t)
     (let ((end (point)))
-      (when (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
-       (narrow-to-region (point) end)))))
+      (cond
+       ((and (not (string-equal footnote-section-tag ""))
+            (re-search-backward
+             (concat "^" footnote-section-tag-regexp) nil t))
+       (narrow-to-region (point) end))
+       (footnote-text-marker-alist
+       (narrow-to-region (cdar footnote-text-marker-alist) end))))))
 
 (defun Footnote-goto-char-point-max ()
   "Move to end of buffer or prior to start of .signature."
@@ -455,7 +509,7 @@ styles."
       (point)))
 
 (defun Footnote-insert-text-marker (arg locn)
-  "Insert a marker pointing to footnote arg, at buffer location locn."
+  "Insert a marker pointing to footnote ARG, at buffer location LOCN."
   (let ((marker (make-marker)))
     (unless (assq arg footnote-text-marker-alist)
       (set-marker marker locn)
@@ -465,7 +519,7 @@ styles."
            (Footnote-sort footnote-text-marker-alist)))))
 
 (defun Footnote-insert-pointer-marker (arg locn)
-  "Insert a marker pointing to footnote arg, at buffer location locn."
+  "Insert a marker pointing to footnote ARG, at buffer location LOCN."
   (let ((marker (make-marker))
        alist)
     (set-marker marker locn)
@@ -478,12 +532,16 @@ styles."
            (Footnote-sort footnote-pointer-marker-alist)))))
 
 (defun Footnote-insert-footnote (arg)
-  "Insert a footnote numbered arg, at (point)."
+  "Insert a footnote numbered ARG, at (point)."
   (push-mark)
   (Footnote-insert-pointer-marker arg (point))
   (Footnote-insert-numbered-footnote arg t)
   (Footnote-goto-char-point-max)
-  (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
+  (if (cond
+       ((not (string-equal footnote-section-tag ""))
+       (re-search-backward (concat "^" footnote-section-tag-regexp) nil t))
+       (footnote-text-marker-alist
+       (goto-char (cdar footnote-text-marker-alist))))
       (save-restriction
        (when footnote-narrow-to-footnotes-when-editing
          (Footnote-narrow-to-footnotes))
@@ -500,12 +558,19 @@ styles."
                                 (regexp-quote footnote-end-tag)))
                       nil t)
                  (unless (beginning-of-line) t))
-               (goto-char (point-max)))))
+               (Footnote-goto-char-point-max)
+               (cond
+                ((not (string-equal footnote-section-tag ""))
+                 (re-search-backward
+                  (concat "^" footnote-section-tag-regexp) nil t))
+                (footnote-text-marker-alist
+                 (goto-char (cdar footnote-text-marker-alist)))))))
     (unless (looking-at "^$")
       (insert "\n"))
     (when (eobp)
       (insert "\n"))
-    (insert footnote-section-tag "\n"))
+    (unless (string-equal footnote-section-tag "")
+      (insert footnote-section-tag "\n")))
   (let ((old-point (point)))
     (Footnote-insert-numbered-footnote arg nil)
     (Footnote-insert-text-marker arg old-point)))
@@ -524,7 +589,8 @@ a footnote."
                   (Footnote-narrow-to-footnotes)
                   (and (>= old-point (point-min))
                        (<= old-point (point-max))))))
-            (>= (point) (cdar footnote-text-marker-alist)))
+            footnote-text-marker-alist
+             (>= (point) (cdar footnote-text-marker-alist)))
     (let ((i 1)
          alist-txt rc)
       (while (and (setq alist-txt (nth i footnote-text-marker-alist))
@@ -576,12 +642,12 @@ If the variable `footnote-narrow-to-footnotes-when-editing' is set,
 the buffer is narrowed to the footnote body.  The restriction is removed
 by using `Footnote-back-to-message'."
   (interactive "*P")
-  (let (num)
-    (if footnote-text-marker-alist
-       (if (< (point) (cadar (last footnote-pointer-marker-alist)))
-           (setq num (Footnote-make-hole))
-         (setq num (1+ (caar (last footnote-text-marker-alist)))))
-      (setq num 1))
+  (let ((num
+         (if footnote-text-marker-alist
+             (if (< (point) (cl-cadar (last footnote-pointer-marker-alist)))
+                 (Footnote-make-hole)
+               (1+ (caar (last footnote-text-marker-alist))))
+           1)))
     (message "Adding footnote %d" num)
     (Footnote-insert-footnote num)
     (insert-before-markers (make-string footnote-body-tag-spacing ? ))
@@ -599,7 +665,7 @@ by using `Footnote-back-to-message'."
 
 (defun Footnote-delete-footnote (&optional arg)
   "Delete a numbered footnote.
-With no parameter, delete the footnote under (point).  With arg specified,
+With no parameter, delete the footnote under (point).  With ARG specified,
 delete the footnote with that number."
   (interactive "*P")
   (unless arg
@@ -616,13 +682,22 @@ delete the footnote with that number."
       (while (car locn)
        (save-excursion
          (goto-char (car locn))
-         (let* ((end (point))
-                (start (search-backward footnote-start-tag nil t)))
-           (kill-region start end)))
+         (when (looking-back (concat (regexp-quote footnote-start-tag)
+                                     (Footnote-current-regexp)
+                                     (regexp-quote footnote-end-tag))
+                             (line-beginning-position))
+           (delete-region (match-beginning 0) (match-end 0))))
        (setq locn (cdr locn)))
       (save-excursion
        (goto-char (cdr alist-txt))
-       (kill-region (point) (search-forward "\n\n" nil t)))
+       (delete-region
+        (point)
+        (if footnote-spaced-footnotes
+            (search-forward "\n\n" nil t)
+          (save-restriction
+            (end-of-line)
+            (next-single-char-property-change
+             (point) 'footnote-number nil (Footnote-goto-char-point-max))))))
       (setq footnote-pointer-marker-alist
            (delq alist-ptr footnote-pointer-marker-alist))
       (setq footnote-text-marker-alist
@@ -631,16 +706,20 @@ delete the footnote with that number."
       (when (and (null footnote-text-marker-alist)
                 (null footnote-pointer-marker-alist))
        (save-excursion
-         (let* ((end (Footnote-goto-char-point-max))
-                (start (1- (re-search-backward
-                            (concat "^" footnote-section-tag-regexp)
-                            nil t))))
-           (forward-line -1)
-           (when (looking-at "\n")
-             (kill-line))
-           (kill-region start (if (< end (point-max))
-                                  end
-                                (point-max)))))))))
+         (if (not (string-equal footnote-section-tag ""))
+             (let* ((end (Footnote-goto-char-point-max))
+                    (start (1- (re-search-backward
+                                (concat "^" footnote-section-tag-regexp)
+                                nil t))))
+               (forward-line -1)
+               (when (looking-at "\n")
+                 (kill-line))
+               (delete-region start (if (< end (point-max))
+                                        end
+                                      (point-max))))
+           (Footnote-goto-char-point-max)
+           (when (looking-back "\n\n")
+             (kill-line -1))))))))
 
 (defun Footnote-renumber-footnotes (&optional arg)
   "Renumber footnotes, starting from 1."
@@ -652,28 +731,31 @@ delete the footnote with that number."
       (while (< i notes)
        (setq alist-ptr (nth i footnote-pointer-marker-alist))
        (setq alist-txt (nth i footnote-text-marker-alist))
-       (unless (eq (1+ i) (car alist-ptr))
+       (unless (= (1+ i) (car alist-ptr))
          (Footnote-renumber (car alist-ptr) (1+ i) alist-ptr alist-txt))
        (setq i (1+ i))))))
 
 (defun Footnote-goto-footnote (&optional arg)
   "Jump to the text of a footnote.
-With no parameter, jump to the text of the footnote under (point).  With arg
+With no parameter, jump to the text of the footnote under (point).  With ARG
 specified, jump to the text of that footnote."
   (interactive "P")
-  (let (footnote)
-    (if arg
-       (setq footnote (assq arg footnote-text-marker-alist))
-      (when (setq arg (Footnote-under-cursor))
-       (setq footnote (assq arg footnote-text-marker-alist))))
-    (if footnote
-       (goto-char (cdr footnote))
-      (if (eq arg 0)
-         (progn
-           (goto-char (point-max))
-           (re-search-backward (concat "^" footnote-section-tag-regexp))
-           (forward-line 1))
-       (error "I don't see a footnote here")))))
+  (unless arg
+    (setq arg (Footnote-under-cursor)))
+  (let ((footnote (assq arg footnote-text-marker-alist)))
+    (cond
+     (footnote
+      (goto-char (cdr footnote)))
+     ((eq arg 0)
+      (goto-char (point-max))
+      (cond
+       ((not (string-equal footnote-section-tag ""))
+       (re-search-backward (concat "^" footnote-section-tag-regexp))
+       (forward-line 1))
+       (footnote-text-marker-alist
+       (goto-char (cdar footnote-text-marker-alist)))))
+     (t
+      (error "I don't see a footnote here")))))
 
 (defun Footnote-back-to-message (&optional arg)
   "Move cursor back to footnote referent.
@@ -687,51 +769,46 @@ being set it is automatically widened."
        (widen))
       (goto-char (cadr (assq note footnote-pointer-marker-alist))))))
 
-(defvar footnote-mode-map nil
-  "Keymap used for footnote minor mode.")
-
-;; Set up our keys
-(unless footnote-mode-map
-  (setq footnote-mode-map (make-sparse-keymap))
-  (define-key footnote-mode-map "a" 'Footnote-add-footnote)
-  (define-key footnote-mode-map "b" 'Footnote-back-to-message)
-  (define-key footnote-mode-map "c" 'Footnote-cycle-style)
-  (define-key footnote-mode-map "d" 'Footnote-delete-footnote)
-  (define-key footnote-mode-map "g" 'Footnote-goto-footnote)
-  (define-key footnote-mode-map "r" 'Footnote-renumber-footnotes)
-  (define-key footnote-mode-map "s" 'Footnote-set-style))
-
-(defvar footnote-minor-mode-map nil
+(defvar footnote-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "a" 'Footnote-add-footnote)
+    (define-key map "b" 'Footnote-back-to-message)
+    (define-key map "c" 'Footnote-cycle-style)
+    (define-key map "d" 'Footnote-delete-footnote)
+    (define-key map "g" 'Footnote-goto-footnote)
+    (define-key map "r" 'Footnote-renumber-footnotes)
+    (define-key map "s" 'Footnote-set-style)
+    map))
+
+(defvar footnote-minor-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map footnote-prefix footnote-mode-map)
+    map)
   "Keymap used for binding footnote minor mode.")
 
-(unless footnote-minor-mode-map
-  (define-key global-map footnote-prefix footnote-mode-map))
-
 ;;;###autoload
-(defun footnote-mode (&optional arg)
-  "Toggle footnote minor mode.
-\\<message-mode-map>
-key            binding
----            -------
-
-\\[Footnote-renumber-footnotes]                Footnote-renumber-footnotes
-\\[Footnote-goto-footnote]             Footnote-goto-footnote
-\\[Footnote-delete-footnote]           Footnote-delete-footnote
-\\[Footnote-cycle-style]               Footnote-cycle-style
-\\[Footnote-back-to-message]           Footnote-back-to-message
-\\[Footnote-add-footnote]              Footnote-add-footnote
-"
-  (interactive "*P")
+(define-minor-mode footnote-mode
+  "Toggle Footnote mode.
+With a prefix argument ARG, enable Footnote mode if ARG is
+positive, and disable it otherwise.  If called from Lisp, enable
+the mode if ARG is omitted or nil.
+
+Footnode mode is a buffer-local minor mode.  If enabled, it
+provides footnote support for `message-mode'.  To get started,
+play around with the following keys:
+\\{footnote-minor-mode-map}"
+  :lighter    footnote-mode-line-string
+  :keymap     footnote-minor-mode-map
   ;; (filladapt-mode t)
-  (setq footnote-mode
-       (if (null arg) (not footnote-mode)
-         (> (prefix-numeric-value arg) 0)))
   (when footnote-mode
     ;; (Footnote-setup-keybindings)
     (make-local-variable 'footnote-style)
-    (if (fboundp 'force-mode-line-update)
-       (force-mode-line-update)
-      (set-buffer-modified-p (buffer-modified-p)))
+    (make-local-variable 'footnote-body-tag-spacing)
+    (make-local-variable 'footnote-spaced-footnotes)
+    (make-local-variable 'footnote-section-tag)
+    (make-local-variable 'footnote-section-tag-regexp)
+    (make-local-variable 'footnote-start-tag)
+    (make-local-variable 'footnote-end-tag)
 
     (when (boundp 'filladapt-token-table)
       ;; add tokens to filladapt to match footnotes
@@ -744,16 +821,8 @@ key                binding
        (unless (assoc bullet-regexp filladapt-token-table)
          (setq filladapt-token-table
                (append filladapt-token-table
-                       (list (list bullet-regexp 'bullet)))))))
-
-    (run-hooks 'footnote-mode-hook)))
-
-(unless (assq 'footnote-mode minor-mode-alist)
-  (setq minor-mode-alist
-       (cons '(footnote-mode footnote-mode-line-string)
-             minor-mode-alist)))
+                       (list (list bullet-regexp 'bullet)))))))))
 
 (provide 'footnote)
 
-;;; arch-tag: 9bcfb6d7-2161-4caf-8793-700f62400398
 ;;; footnote.el ends here