]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-footnote.el
Merge from emacs-23
[gnu-emacs] / lisp / org / org-footnote.el
index d606049336c6ba93457737faa8ed1a841cf91951..24ec581e75cf1259502b09bcf6da97527fb21b45 100644 (file)
@@ -5,7 +5,7 @@
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
 ;; Homepage: http://orgmode.org
-;; Version: 6.33x
+;; Version: 7.4
 ;;
 ;; This file is part of GNU Emacs.
 ;;
@@ -47,7 +47,9 @@
 (declare-function org-back-to-heading "org" (&optional invisible-ok))
 (declare-function org-end-of-subtree "org"  (&optional invisible-ok to-heading))
 (declare-function org-in-verbatim-emphasis "org" ())
+(declare-function org-inside-latex-macro-p "org" ())
 (defvar org-odd-levels-only) ;; defined in org.el
+(defvar message-signature-separator) ;; defined in message.el
 
 (defconst org-footnote-re
   (concat "[^][\n]"   ; to make sure it is not at the beginning of a line
   (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
   "Regular expression matching the definition of a footnote.")
 
+(defgroup org-footnote nil
+  "Footnotes in Org-mode."
+  :tag "Org Footnote"
+  :group 'org)
+
 (defcustom org-footnote-section "Footnotes"
   "Outline heading containing footnote definitions before export.
 This can be nil, to place footnotes locally at the end of the current
@@ -74,7 +81,7 @@ automatically, i.e. when creating the footnote, and when sorting the notes.
 However, by hand you may place definitions *anywhere*.
 If this is a string, during export, all subtrees starting with this
 heading will be removed after extracting footnote definitions."
-  :group 'org-footnotes
+  :group 'org-footnote
   :type '(choice
          (string :tag "Collect footnotes under heading")
          (const :tag "Define footnotes locally" nil)))
@@ -86,11 +93,11 @@ as in Org-mode.  Outside Org-mode, new footnotes are always placed at
 the end of the file.  When you normalize the notes, any line containing
 only this tag will be removed, a new one will be inserted at the end
 of the file, followed by the collected and normalized footnotes."
-  :group 'org-footnotes
+  :group 'org-footnote
   :type 'string)
 
 (defcustom org-footnote-define-inline nil
-  "Non-nil means, define footnotes inline, at reference location.
+  "Non-nil means define footnotes inline, at reference location.
 When nil, footnotes will be defined in a special section near
 the end of the document.  When t, the [fn:label:definition] notation
 will be used to define the footnote at the reference position."
@@ -98,7 +105,7 @@ will be used to define the footnote at the reference position."
   :type 'boolean)
 
 (defcustom org-footnote-auto-label t
-  "Non-nil means, define automatically new labels for footnotes.
+  "Non-nil means define automatically new labels for footnotes.
 Possible values are:
 
 nil        prompt the user for each label
@@ -115,7 +122,7 @@ plain      Automatically create plain number labels like [1]"
          (const :tag "Create automatic [N]" plain)))
 
 (defcustom org-footnote-auto-adjust nil
-  "Non-nil means, automatically adjust footnotes after insert/delete.
+  "Non-nil means automatically adjust footnotes after insert/delete.
 When this is t, after each insertion or deletion of a footnote,
 simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
 If you want to have just sorting or just renumbering, set this variable
@@ -132,7 +139,7 @@ The main values of this variable can be set with in-buffer options:
          (const :tag "Renumber and Sort" t)))
 
 (defcustom org-footnote-fill-after-inline-note-extraction nil
-  "Non-nil means, fill paragraphs after extracting footnotes.
+  "Non-nil means fill paragraphs after extracting footnotes.
 When extracting inline footnotes, the lengths of lines can change a lot.
 When this option is set, paragraphs from which an inline footnote has been
 extracted will be filled again."
@@ -181,25 +188,25 @@ with start and label of the footnote if there is a definition at point."
       (org-show-context 'link-search)
       (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
 
-(defun org-footnote-goto-next-reference (label)
-  "Find the definition of the footnote with label LABEL."
+(defun org-footnote-goto-previous-reference (label)
+  "Find the first closest (to point) reference of footnote with label LABEL."
   (interactive "sLabel: ")
   (org-mark-ring-push)
   (setq label (org-footnote-normalize-label label))
   (let ((re (format ".\\[%s[]:]" label))
        (p0 (point)) pos)
     (save-excursion
-      (setq pos (or (re-search-forward re nil t)
-                   (and (goto-char (point-min))
-                        (re-search-forward re nil t))
+      (setq pos (or (re-search-backward re nil t)
+                   (and (goto-char (point-max))
+                        (re-search-backward re nil t))
                    (and (progn (widen) t)
                         (goto-char p0)
-                        (re-search-forward re nil t))
-                   (and (goto-char (point-min))
+                        (re-search-backward re nil t))
+                   (and (goto-char (point-max))
                         (re-search-forward re nil t)))))
     (if pos
        (progn
-         (goto-char pos)
+         (goto-char (match-end 0))
          (org-show-context 'link-search))
       (error "Cannot find reference of footnote %s" label))))
 
@@ -296,15 +303,19 @@ or new, let the user edit the definition of the footnote."
      (t
       (setq re (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
       (unless (re-search-forward re nil t)
-       (goto-char (point-max))
-       (skip-chars-backward " \t\r\n")
-       (insert "\n\n")
-       (delete-region (point) (point-max))
-       (insert org-footnote-tag-for-non-org-mode-files "\n"))
-      (goto-char (point-max))
-      (skip-chars-backward " \t\r\n")))
-    (insert "\n\n")
-    (insert "[" label "] ")
+       (let ((max (if (and (derived-mode-p 'message-mode)
+                           (re-search-forward message-signature-separator nil t))
+                      (progn (beginning-of-line) (point))
+                    (goto-char (point-max)))))
+         (skip-chars-backward " \t\r\n")
+         (delete-region (point) max)
+         (insert "\n\n")
+         (insert org-footnote-tag-for-non-org-mode-files "\n")))))
+    ;; Skip existing footnotes
+    (while (re-search-forward "^[[:space:]]*\\[[^]]+\\] " nil t)
+      (forward-line))
+    (insert "[" label "] \n")
+    (goto-char (1- (point)))
     (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
 
 ;;;###autoload
@@ -338,7 +349,7 @@ With prefix arg SPECIAL, offer additional commands in a menu."
          (org-footnote-goto-definition (nth 1 tmp))
        (goto-char (match-beginning 4))))
      ((setq tmp (org-footnote-at-definition-p))
-      (org-footnote-goto-next-reference (nth 1 tmp)))
+      (org-footnote-goto-previous-reference (nth 1 tmp)))
      (t (org-footnote-new)))))
 
 ;;;###autoload
@@ -366,7 +377,8 @@ referenced sequence."
       ;; Now find footnote references, and extract the definitions
       (goto-char (point-min))
       (while (re-search-forward org-footnote-re nil t)
-       (unless (or (org-in-commented-line) (org-in-verbatim-emphasis))
+       (unless (or (org-in-commented-line) (org-in-verbatim-emphasis)
+                   (org-inside-latex-macro-p))
          (org-if-unprotected
           (setq def (match-string 4)
                 idef def
@@ -397,13 +409,13 @@ referenced sequence."
                   (skip-chars-backward " \t\n\t")
                   (delete-region (1+ (point)) (match-beginning 0))))))
           (unless sort-only
-            (replace-match (concat before "[" marker "]"))
+            (replace-match (concat before "[" marker "]") t t)
             (and idef
                  org-footnote-fill-after-inline-note-extraction
                  (fill-paragraph)))
           (if (not a) (push (list ref marker def (if idef t nil))
                             ref-table)))))
-      
+
       ;; First find and remove the footnote section
       (goto-char (point-min))
       (cond
@@ -499,7 +511,8 @@ ENTRY is (fn-label num-mark definition)."
     (beginning-of-line 0))
   (if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
   (end-of-line 1)
-  (skip-chars-backward "\n\r\t "))
+  (skip-chars-backward "\n\r\t ")
+  (forward-line))
 
 (defun org-footnote-delete (&optional label)
   "Delete the footnote at point.