mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-02-28 02:40:46 -08:00
* org-protocol.el (org-protocol-store-link) (org-protocol-remember, org-protocol-open-source): Add autoloads. * org-compat.el (org-float-time): New function. * org.el (org-clock-update-time-maybe) (org-sort-entries-or-items, org-do-sort) (org-evaluate-time-range, org-time-string-to-seconds) (org-closed-in-range): Use `org-float-time'. * org-timer.el (org-timer-start, org-timer-pause-or-continue) (org-timer-seconds): Use `org-float-time'. * org-clock.el (org-clock-get-clocked-time, org-clock-out) (org-clock-sum, org-dblock-write:clocktable) (org-clocktable-steps): Use `org-float-time'. * org-agenda.el (org-agenda-last-marker-time) (org-agenda-new-marker, org-diary): Use `org-float-time'. * org-compat.el (w32-focus-frame): Declare the w32-focus-frame function. * org-exp.el (org-get-file-contents): Only protect lines that really need it. * org-html.el (require): Require cl for compilation. * org.el: Avoid using `default-major-mode'. * org-plot.el (require): Require CL only at compile time. * org-exp.el (require): Require CL only at compile time. * org-agenda.el (org-agenda-quit): When the agenda window is dedicated, remove other windows before exiting, so that the frame really will be killed. * org-exp.el (org-export-handle-include-files): Reset START and END for each loop cycle. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-eval-in-calendar): Use `org-select-frame-set-input-focus'. * org-compat.el (org-select-frame-set-input-focus): New function. * org.el (org-update-statistics-cookies): New function. (org-mode-map): Bind `C-c #' to `org-update-statistics-cookies'. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-src.el (org-edit-fixed-width-region): Set org-src-mode only after the local variables are set. * org-latex.el (org-export-latex-protect-amp): New function. (org-export-latex-links): Protect link ampersands in tables. * org-exp.el (org-export-select-backend-specific-text): Match in two steps, to avoid regexp problems. * org.el (org-offer-links-in-entry): Improve working with many and duplicate links. * org-agenda.el (org-agenda-show-1): Make more consistent with normal cycling. (org-agenda-cycle-show): Make more consistent with normal cycling. * org-gnus.el (org-gnus-store-link): Restore the linking to a website. 2009-09-02 Bastien Guerry <bzg@altern.org> * org-latex.el (org-export-latex-first-lines): Bugfix. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-clock.el (org-clock-modify-effort-estimate): Emit message about new effort. * org.el (org-set-effort): New function. (org-mode-map): New key for effort setting command. * org-agenda.el (org-agenda): Keep window setup when calling agenda from within agenda window. (org-agenda-mode-map): New keys for effort setting commands. (org-agenda-menu): Add effort setting commands to menu. (org-agenda-set-property, org-agenda-set-effort): New functions. * org-latex.el (org-export-latex-tables): Fix `org-table-last-alignment' and `org-table-last-column-widths' if the first column has been removed. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-remove-timestamp-with-keyword): Only remove in entry, not in subtree. * org-src.el (org-src-lang-modes): Add abbreviation elisp for emacs lisp. * org.el (org-open-at-point): When on headline, offer all strings in entry. * org-remember.el (org-remember-templates): Documentation fix. * org.el (org-move-subtree-down): Use `org-get-next-sibling' and `org-get-last-sibling' instead of the outline versions of these functions. (org-get-last-sibling): New function. (org-refile): Use `org-get-next-sibling' instead of the outline version of this function. (org-clean-visibility-after-subtree-move): Use `org-get-next-sibling' and `org-get-last-sibling' instead of the outline versions of these functions. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-prepare-agenda): When creating a new frame for the agenda, make the window dedicated. * org-agenda.el (org-agenda-mode-map): New keys for time motion. * org-table.el (org-table-align): Change the order of reinsertion and deletion, to avoid problems with overlays following the table. * org.el (org-parse-time-string): Better error message. (org-show-subtree): Use org-end-of-subtree. * org-macs.el (org-goto-line): New defsubst. * org.el (org-open-file, org-change-tag-in-region) (org-fast-tag-show-exit): Don't use `goto-line'. * org-table.el (org-table-align, org-table-insert-column) (org-table-delete-column, org-table-move-column) (org-table-sort-lines, org-table-copy-region) (org-table-paste-rectangle, org-table-wrap-region) (org-table-get-specials, org-table-rotate-recalc-marks) (org-table-get-range, org-table-recalculate) (org-table-edit-formulas, org-table-fedit-convert-buffer) (org-table-show-reference, org-table-highlight-rectangle): Don't use `goto-line'. * org-src.el (org-edit-src-code, org-edit-fixed-width-region) (org-edit-src-exit): Don't use `goto-line'. * org-macs.el (org-preserve-lc): Don't use `goto-line'. * org-list.el (org-renumber-ordered-list, org-fix-bullet-type): Don't use `goto-line'. * org-exp.el (org-export-number-lines): Don't use `goto-line'. * org-colview.el (org-columns, org-columns-redo) (org-agenda-columns): Don't use `goto-line'. * org-colview-xemacs.el (org-columns, org-agenda-columns): Don't use `goto-line'. * org-agenda.el (org-agenda-mode): Force visual line motion off. (org-agenda-add-entry-text-maxlines): Improve docstring. (org-agenda-start-with-entry-text-mode): New option. (org-agenda-entry-text-maxlines): New option. (org-agenda-entry-text-mode): New variable. (org-agenda-mode): Set initial value of `org-agenda-entry-text-mode'. (org-agenda-mode-map): Add the `E' key. (org-agenda-menu): Add entry text mode to the menu. (org-agenda-get-some-entry-text): Fix line count bug. (org-finalize-agenda): Apply entry text mode if appropriate. (org-agenda-entry-text-show-here): New function. (org-agenda-entry-text-show): New function. (org-agenda-entry-text-hide): New function. (org-agenda-view-mode-dispatch): Add entry text mode to the view key menu. (org-agenda-entry-text-mode): New command. (org-agenda-set-mode-name): Add entry text mode to the mode line string. (org-agenda-undo, org-agenda-get-restriction-and-command) (org-agenda-get-some-entry-text, org-agenda-redo): Don't use `goto-line'. 2009-09-02 Bernt Hansen <bernt@norang.ca> * org-clock.el (org-notify): Bugfix. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-agenda-open-link): Handle multiple links and check for after-string. * org-gnus.el (org-gnus-store-link): Simplify. * org.el (org-latex-regexps): Don't add extra empty lines for display formulas. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-agenda-get-some-entry-text): New function. (org-agenda-add-entry-text): Use `org-agenda-get-some-entry-text'. * org.el (org-cycle-separator-lines): Update docstring. (org-cycle-show-empty-lines): Handle negative values for `org-cycle-show-empty-lines'. * org-exp.el (org-export-protect-sub-super): New function. (org-export-normalize-links): Protect the url of plain links from supscript and superscript processing. * org-remember.el (org-remember-escaped-%): New function. (org-remember-apply-template): Use `org-remember-escaped-%' to detect escaped % signs. 2009-09-02 Bastien Guerry <bzg@altern.org> * org-timer.el (org-timer-set-timer): Use `org-notify' and play a sound when showing the notification. * org-clock.el (org-notify): New function. (org-clock-notify-once-if-expired): Use `org-notify'. * org-gnus.el (org-gnus-store-link): Handle `gnus-summary-mode' and `gnus-article-mode' separately. (gnus-summary-article-header): Fix the declare-function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-exp.el (org-export-format-source-code-or-example): Translate language. * org-src.el (org-src-lang-modes): New variable (org-edit-src-code): Translate language. * org-exp.el (org-export-format-source-code-or-example): Deal wit the new structure of the `org-export-latex-listings-langs' variable. * org-latex.el (org-export-latex-listings-langs): Change structure of the variable from plist to alist. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-in-commented-line): New function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-hide-block-toggle): Make folded blocks searchable. 2009-09-02 Friedrich Delgado Friedrichs <friedel@nomaden.org> (tiny change) * org.el (org-flag-drawer): More useful error. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-remember.el (org-remember-apply-template): Use org-icompleting-read. * org-publish.el (org-publish): Use org-icompleting-read. * org-colview.el (org-columns-edit-value, org-columns-new) (org-insert-columns-dblock): Use org-icompleting-read. * org-colview-xemacs.el (org-columns-edit-value) (org-columns-new, org-insert-columns-dblock): Use org-icompleting-read. * org-attach.el (org-attach-delete-one, org-attach-open): Use org-icompleting-read. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-hierarchical-todo-statistics): Improve docstring. (org-version): Return the version text. (org-org-menu): Add a menu entry for the new bug reporter. (org-submit-bug-report): New command. * org-list.el (org-hierarchical-checkbox-statistics): Improve docstring. * org.el (org-emphasis-regexp-components): Add "`" to set of pre-emphasis characters. * org-latex.el (org-export-latex-classes): Always include the soul package. (org-export-latex-emphasis-alist): Use \st for strikethough. * org-exp-blocks.el (org-export-blocks-preprocess): Use `indent-code-rigidly' to indent. * org-agenda.el (org-agenda-get-restriction-and-command): Remove properties only if MATCH really is a string. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-latex.el (org-export-latex-packages-alist): Fix customization type. * org.el (org-create-formula-image): Also use `org-export-latex-packages-alist'. * org-html.el (org-export-as-html): Fix bug in footnote regexp. (org-export-as-html): Format footnotes correctly. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-fast-tag-selection): Avoid text properties on tags in the alist. * org-agenda.el (org-agenda-get-restriction-and-command): Avoid text properties on the match element. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-set-regexps-and-options): Make sure the list of done keywords is not invalid. * org-exp.el (org-export-interpolate-newlines): New function. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-format-latex): Avoid nested overlays. * org-latex.el (org-export-latex-listings-langs): Add a few more languages. * org-exp.el (org-export-preprocess-apply-macros): Make sure to ignore newlines and space before the first macro argument. * org-latex.el (org-export-latex-tables): Remove save-excursion around `org-table-align'. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-export-html-special-string-regexps): Definition moved into org.el * org-exp.el (org-export-preprocess-apply-macros): Allow newlines in macro calls. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-latex.el (org-export-latex-listings) (org-export-latex-listings-langs): New options. * org-exp.el (org-export-format-source-code-or-example): Use listing package if requested by the user. 2009-09-02 Bastien Guerry <bzg@altern.org> * org.el (org-iswitchb): Fix bug when aborting the `org-iswitchb' command before actually switching to a buffer. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org-exp.el (org-get-file-contents): Only quote org lines when the markup is src or example. * org-agenda.el (org-agenda-skip-scheduled-if-deadline-is-shown): New option (org-agenda-get-day-entries): Remember deadline results and pass them on into the function getting the scheduling information. (org-agenda-get-scheduled): Accept deadline results as parameters and maybe skip some entries. (org-agenda-skip-scheduled-if-deadline-is-shown): New option. * org.el (org-insert-heading): When respecting content, do not convert current line to headline. * org-clock.el (org-clock-save-markers-for-cut-and-paste): Also cheeeeeck the hd marker (org-clock-in): Also set the hd marker. (org-clock-out): Also set the hd marker. (org-clock-cancel): Reset markers. * org.el (org-clock-hd-marker): New marker. * org-faces.el (org-agenda-clocking): New face. * org-agenda.el (org-agenda-mark-clocking-task): New function. (org-finalize-agenda): call `org-agenda-mark-clocking-task'. * org.el (org-modules): Add org-track.el. * org-agenda.el (org-agenda-bulk-marked-p): New function. (org-agenda-bulk-mark, org-agenda-bulk-unmark): Use `org-agenda-bulk-marked-p'. (org-agenda-bulk-toggle): New command. 2009-09-02 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-move-subtree-down): Hide subtree if it was folded, not just the body. * org-remember.el (org-remember-finalize): Avoid buffer-modified messages.
575 lines
21 KiB
EmacsLisp
575 lines
21 KiB
EmacsLisp
;;; org-footnote.el --- Footnote support in Org and elsewhere
|
|
;;
|
|
;; Copyright (C) 2009 Free Software Foundation, Inc.
|
|
;;
|
|
;; Author: Carsten Dominik <carsten at orgmode dot org>
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
|
;; Homepage: http://orgmode.org
|
|
;; Version: 6.30c
|
|
;;
|
|
;; 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 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.
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;;
|
|
;;; Commentary:
|
|
|
|
;; This file contains the code dealing with footnotes in Org-mode.
|
|
;; The code can also be used in arbitrary text modes to provide
|
|
;; footnotes. Compared to Steven L Baur's footnote.el it provides
|
|
;; better support for resuming editing. It is less configurable than
|
|
;; Steve's code, though.
|
|
|
|
;;; Code:
|
|
|
|
(eval-when-compile
|
|
(require 'cl))
|
|
(require 'org-macs)
|
|
(require 'org-compat)
|
|
|
|
(declare-function org-in-commented-line "org" ())
|
|
(declare-function org-in-regexp "org" (re &optional nlines visually))
|
|
(declare-function org-mark-ring-push "org" (&optional pos buffer))
|
|
(declare-function outline-next-heading "outline")
|
|
(declare-function org-trim "org" (s))
|
|
(declare-function org-show-context "org" (&optional key))
|
|
(declare-function org-back-to-heading "org" (&optional invisible-ok))
|
|
(declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
|
|
(defvar org-odd-levels-only) ;; defined in org.el
|
|
|
|
(defconst org-footnote-re
|
|
(concat "[^][\n]" ; to make sure it is not at the beginning of a line
|
|
"\\["
|
|
"\\(?:"
|
|
"\\([0-9]+\\)"
|
|
"\\|"
|
|
(org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
|
|
"\\)"
|
|
"\\]")
|
|
"Regular expression for matching footnotes.")
|
|
|
|
(defconst org-footnote-definition-re
|
|
(org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
|
|
"Regular expression matching the definition of a footnote.")
|
|
|
|
(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
|
|
outline node. If can also be the name of a special outline heading
|
|
under which footnotes should be put.
|
|
This variable defines the place where Org puts the definition
|
|
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
|
|
:type '(choice
|
|
(string :tag "Collect fotnotes under heading")
|
|
(const :tag "Define footnotes locally" nil)))
|
|
|
|
(defcustom org-footnote-tag-for-non-org-mode-files "Footnotes:"
|
|
"Tag marking the beginning of footnote section.
|
|
The Org-mode footnote engine can be used in arbitrary text files as well
|
|
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
|
|
:type 'string)
|
|
|
|
(defcustom org-footnote-define-inline nil
|
|
"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."
|
|
:group 'org-footnote
|
|
:type 'boolean)
|
|
|
|
(defcustom org-footnote-auto-label t
|
|
"Non-nil means, define automatically new labels for footnotes.
|
|
Possible values are:
|
|
|
|
nil prompt the user for each label
|
|
t create unique labels of the form [fn:1], [fn:2], ...
|
|
confirm like t, but let the user edit the created value. In particular,
|
|
the label can be removed from the minibuffer, to create
|
|
an anonymous footnote.
|
|
plain Automatically create plain number labels like [1]"
|
|
:group 'org-footnote
|
|
:type '(choice
|
|
(const :tag "Frompt for label" nil)
|
|
(const :tag "Create automatic [fn:N]" t)
|
|
(const :tag "Offer automatic [fn:N] for editing" confirm)
|
|
(const :tag "Create automatic [N]" plain)))
|
|
|
|
(defcustom org-footnote-auto-adjust nil
|
|
"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
|
|
to `sort' or `renumber'.
|
|
|
|
The main values of this variable can be set with in-buffer options:
|
|
|
|
#+STARTUP: fnadjust
|
|
#+STARTUP: nofnadjust"
|
|
:group 'org-footnote
|
|
:type '(choice
|
|
(const :tag "Renumber" renumber)
|
|
(const :tag "Sort" sort)
|
|
(const :tag "Renumber and Sort" t)))
|
|
|
|
(defcustom org-footnote-fill-after-inline-note-extraction nil
|
|
"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."
|
|
:group 'org-footnote
|
|
:type 'boolean)
|
|
|
|
(defun org-footnote-at-reference-p ()
|
|
"Is the cursor at a footnote reference?
|
|
If yes, return the beginning position, the label, and the definition, if local."
|
|
(when (org-in-regexp org-footnote-re 15)
|
|
(list (match-beginning 0)
|
|
(or (match-string 1)
|
|
(if (equal (match-string 2) "fn:") nil (match-string 2)))
|
|
(match-string 4))))
|
|
|
|
(defun org-footnote-at-definition-p ()
|
|
"Is the cursor at a footnote definition.
|
|
This matches only pure definitions like [1] or [fn:name] at the beginning
|
|
of a line. It does not a references like [fn:name:definition], where the
|
|
footnote text is included and defined locally.
|
|
The return value will be nil if not at a footnote definition, and a list
|
|
with start and label of the footnote if there is a definition at point."
|
|
(save-excursion
|
|
(end-of-line 1)
|
|
(let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t))))
|
|
(when (re-search-backward org-footnote-definition-re lim t)
|
|
(list (match-beginning 0) (match-string 2))))))
|
|
|
|
(defun org-footnote-goto-definition (label)
|
|
"Find the definition of the footnote with label LABEL."
|
|
(interactive "sLabel: ")
|
|
(org-mark-ring-push)
|
|
(setq label (org-footnote-normalize-label label))
|
|
(let ((re (format "^\\[%s\\]\\|.\\[%s:" label label))
|
|
pos)
|
|
(save-excursion
|
|
(setq pos (or (re-search-forward re nil t)
|
|
(and (goto-char (point-min))
|
|
(re-search-forward re nil t))
|
|
(and (progn (widen) t)
|
|
(goto-char (point-min))
|
|
(re-search-forward re nil t)))))
|
|
(if (not pos)
|
|
(error "Cannot find definition of footnote %s" label)
|
|
(goto-char pos)
|
|
(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."
|
|
(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))
|
|
(and (progn (widen) t)
|
|
(goto-char p0)
|
|
(re-search-forward re nil t))
|
|
(and (goto-char (point-min))
|
|
(re-search-forward re nil t)))))
|
|
(if pos
|
|
(progn
|
|
(goto-char pos)
|
|
(org-show-context 'link-search))
|
|
(error "Cannot find reference of footnote %s" label))))
|
|
|
|
(defun org-footnote-normalize-label (label)
|
|
(if (numberp label) (setq label (number-to-string label)))
|
|
(if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label))
|
|
(setq label (concat "fn:" label)))
|
|
label)
|
|
|
|
(defun org-footnote-all-labels ()
|
|
"Return list with all defined foot labels used in the buffer."
|
|
(let (rtn l)
|
|
(save-excursion
|
|
(save-restriction
|
|
(widen)
|
|
(goto-char (point-min))
|
|
(while (re-search-forward org-footnote-definition-re nil t)
|
|
(setq l (org-match-string-no-properties 2))
|
|
(and l (add-to-list 'rtn l)))
|
|
(goto-char (point-min))
|
|
(while (re-search-forward org-footnote-re nil t)
|
|
(setq l (or (org-match-string-no-properties 1)
|
|
(org-match-string-no-properties 2)))
|
|
(and l (not (equal l "fn:")) (add-to-list 'rtn l)))))
|
|
rtn))
|
|
|
|
(defun org-footnote-unique-label (&optional current)
|
|
"Return a new unique footnote label.
|
|
The returns the firsts fn:N labels that is currently not used."
|
|
(unless current (setq current (org-footnote-all-labels)))
|
|
(let ((fmt (if (eq org-footnote-auto-label 'plain) "%d" "fn:%d"))
|
|
(cnt 1))
|
|
(while (member (format fmt cnt) current)
|
|
(incf cnt))
|
|
(format fmt cnt)))
|
|
|
|
(defvar org-footnote-label-history nil
|
|
"History of footnote labels entered in current buffer.")
|
|
(make-variable-buffer-local 'org-footnote-label-history)
|
|
|
|
(defun org-footnote-new ()
|
|
"Insert a new footnote.
|
|
This command prompts for a label. If this is a label referencing an
|
|
existing label, only insert the label. If the footnote label is empty
|
|
or new, let the user edit the definition of the footnote."
|
|
(interactive)
|
|
(let* ((labels (org-footnote-all-labels))
|
|
(propose (org-footnote-unique-label labels))
|
|
(label
|
|
(if (member org-footnote-auto-label '(t plain))
|
|
propose
|
|
(completing-read
|
|
"Label (leave empty for anonymous): "
|
|
(mapcar 'list labels) nil nil
|
|
(if (eq org-footnote-auto-label 'confirm) propose nil)
|
|
'org-footnote-label-history))))
|
|
(setq label (org-footnote-normalize-label label))
|
|
(cond
|
|
((equal label "")
|
|
(insert "[fn:: ]")
|
|
(backward-char 1))
|
|
((member label labels)
|
|
(insert "[" label "]")
|
|
(message "New reference to existing note"))
|
|
(org-footnote-define-inline
|
|
(insert "[" label ": ]")
|
|
(backward-char 1)
|
|
(org-footnote-auto-adjust-maybe))
|
|
(t
|
|
(insert "[" label "]")
|
|
(org-footnote-create-definition label)
|
|
(org-footnote-auto-adjust-maybe)))))
|
|
|
|
(defun org-footnote-create-definition (label)
|
|
"Start the definition of a footnote with label LABEL."
|
|
(interactive "sLabel: ")
|
|
(setq label (org-footnote-normalize-label label))
|
|
(let (re)
|
|
(cond
|
|
((org-mode-p)
|
|
(if (not org-footnote-section)
|
|
;; No section, put footnote into the current outline node
|
|
nil
|
|
;; Try to find or make the special node
|
|
(setq re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$"))
|
|
(unless (or (re-search-forward re nil t)
|
|
(and (progn (widen) t)
|
|
(re-search-forward re nil t)))
|
|
(goto-char (point-max))
|
|
(insert "\n\n* " org-footnote-section "\n")))
|
|
;; Now go to the end of this entry and insert there.
|
|
(org-footnote-goto-local-insertion-point))
|
|
(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 "] ")
|
|
(message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
|
|
|
|
;;;###autoload
|
|
(defun org-footnote-action (&optional special)
|
|
"Do the right thing for footnotes.
|
|
When at a footnote reference, jump to the definition. When at a definition,
|
|
jump to the refernces. When neither at definition or reference,
|
|
create a new footnote, interactively.
|
|
With prefix arg SPECIAL, offer additional commands in a menu."
|
|
(interactive "P")
|
|
(let (tmp c)
|
|
(cond
|
|
(special
|
|
(message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
|
|
(setq c (read-char-exclusive))
|
|
(cond
|
|
((equal c ?s)
|
|
(org-footnote-normalize 'sort))
|
|
((equal c ?r)
|
|
(org-footnote-renumber-fn:N))
|
|
((equal c ?S)
|
|
(org-footnote-renumber-fn:N)
|
|
(org-footnote-normalize 'sort))
|
|
((equal c ?n)
|
|
(org-footnote-normalize))
|
|
((equal c ?d)
|
|
(org-footnote-delete))
|
|
(t (error "No such footnote command %c" c))))
|
|
((setq tmp (org-footnote-at-reference-p))
|
|
(if (nth 1 tmp)
|
|
(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)))
|
|
(t (org-footnote-new)))))
|
|
|
|
;;;###autoload
|
|
(defun org-footnote-normalize (&optional sort-only for-preprocessor)
|
|
"Collect the footnotes in various formats and normalize them.
|
|
This finds the different sorts of footnotes allowed in Org, and
|
|
normalizes them to the usual [N] format that is understood by the
|
|
Org-mode exporters.
|
|
When SORT-ONLY is set, only sort the footnote definitions into the
|
|
referenced sequence."
|
|
;; This is based on Paul's function, but rewritten.
|
|
(let* ((limit-level
|
|
(and (boundp 'org-inlinetask-min-level)
|
|
org-inlinetask-min-level
|
|
(1- org-inlinetask-min-level)))
|
|
(nstars (and limit-level
|
|
(if org-odd-levels-only
|
|
(and limit-level (1- (* limit-level 2)))
|
|
limit-level)))
|
|
(outline-regexp
|
|
(concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))
|
|
(count 0)
|
|
ref def idef ref-table beg beg1 marker a before ins-point)
|
|
(save-excursion
|
|
;; Now find footnote references, and extract the definitions
|
|
(goto-char (point-min))
|
|
(while (re-search-forward org-footnote-re nil t)
|
|
(unless (org-in-commented-line)
|
|
(org-if-unprotected
|
|
(setq def (match-string 4)
|
|
idef def
|
|
ref (or (match-string 1) (match-string 2))
|
|
before (char-to-string (char-after (match-beginning 0))))
|
|
(if (equal ref "fn:") (setq ref nil))
|
|
(if (and ref (setq a (assoc ref ref-table)))
|
|
(progn
|
|
(setq marker (nth 1 a))
|
|
(unless (nth 2 a) (setf (caddr a) def)))
|
|
(setq marker (number-to-string (incf count))))
|
|
(save-match-data
|
|
(if def
|
|
(setq def (org-trim def))
|
|
(save-excursion
|
|
(goto-char (point-min))
|
|
(if (not (re-search-forward (concat "^\\[" (regexp-quote ref)
|
|
"\\]") nil t))
|
|
(setq def nil)
|
|
(setq beg (match-beginning 0))
|
|
(setq beg1 (match-end 0))
|
|
(re-search-forward
|
|
(org-re "^[ \t]*$\\|^\\*+ \\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
|
|
nil 'move)
|
|
(setq def (buffer-substring beg1 (or (match-beginning 0)
|
|
(point-max))))
|
|
(goto-char beg)
|
|
(skip-chars-backward " \t\n\t")
|
|
(delete-region (1+ (point)) (match-beginning 0))))))
|
|
(unless sort-only
|
|
(replace-match (concat before "[" marker "]"))
|
|
(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
|
|
((org-mode-p)
|
|
(if (and org-footnote-section
|
|
(re-search-forward
|
|
(concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
|
|
"[ \t]*$")
|
|
nil t))
|
|
(if (or for-preprocessor (not org-footnote-section))
|
|
(replace-match "")
|
|
(org-back-to-heading t)
|
|
(forward-line 1)
|
|
(setq ins-point (point))
|
|
(delete-region (point) (org-end-of-subtree t)))
|
|
(goto-char (point-max))
|
|
(unless for-preprocessor
|
|
(when org-footnote-section
|
|
(or (bolp) (insert "\n"))
|
|
(insert "* " org-footnote-section "\n")
|
|
(setq ins-point (point))))))
|
|
(t
|
|
(if (re-search-forward
|
|
(concat "^"
|
|
(regexp-quote org-footnote-tag-for-non-org-mode-files)
|
|
"[ \t]*$")
|
|
nil t)
|
|
(replace-match ""))
|
|
(goto-char (point-max))
|
|
(skip-chars-backward " \t\n\r")
|
|
(delete-region (point) (point-max))
|
|
(insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n")
|
|
(setq ins-point (point))))
|
|
|
|
;; Insert the footnotes again
|
|
(goto-char (or ins-point (point-max)))
|
|
(setq ref-table (reverse ref-table))
|
|
(when sort-only
|
|
;; remove anonymous and inline footnotes from the list
|
|
(setq ref-table
|
|
(delq nil (mapcar
|
|
(lambda (x) (and (car x)
|
|
(not (equal (car x) "fn:"))
|
|
(not (nth 3 x))
|
|
x))
|
|
ref-table))))
|
|
;; Make sure each footnote has a description, or an error message.
|
|
(setq ref-table
|
|
(mapcar
|
|
(lambda (x)
|
|
(if (not (nth 2 x))
|
|
(setcar (cddr x)
|
|
(format "FOOTNOTE DEFINITION NOT FOUND: %s" (car x)))
|
|
(setcar (cddr x) (org-trim (nth 2 x))))
|
|
x)
|
|
ref-table))
|
|
|
|
(if (or (not (org-mode-p)) ; not an Org file
|
|
org-footnote-section ; we do not use a footnote section
|
|
(not sort-only) ; this is normalization
|
|
for-preprocessor) ; the is the preprocessor
|
|
;; Insert the footnotes together in one place
|
|
(progn
|
|
(setq def
|
|
(mapconcat
|
|
(lambda (x)
|
|
(format "[%s] %s" (nth (if sort-only 0 1) x)
|
|
(org-trim (nth 2 x))))
|
|
ref-table "\n\n"))
|
|
(if ref-table (insert "\n" def "\n\n")))
|
|
;; Insert each footnote near the first reference
|
|
;; Happens only in Org files with no special footnote section,
|
|
;; and only when doing sorting
|
|
(mapc 'org-insert-footnote-reference-near-definition
|
|
ref-table)))))
|
|
|
|
(defun org-insert-footnote-reference-near-definition (entry)
|
|
"Find first reference of footnote ENTRY and insert the definition there.
|
|
ENTRY is (fn-label num-mark definition)."
|
|
(when (car entry)
|
|
(goto-char (point-min))
|
|
(when (re-search-forward (format ".\\[%s[]:]" (regexp-quote (car entry)))
|
|
nil t)
|
|
(org-footnote-goto-local-insertion-point)
|
|
(insert (format "\n\n[%s] %s" (car entry) (nth 2 entry))))))
|
|
|
|
(defun org-footnote-goto-local-insertion-point ()
|
|
"Find insertion point for footnote, just before next outline heading."
|
|
(org-with-limited-levels (outline-next-heading))
|
|
(or (bolp) (newline))
|
|
(beginning-of-line 0)
|
|
(while (and (not (bobp)) (= (char-after) ?#))
|
|
(beginning-of-line 0))
|
|
(if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
|
|
(end-of-line 1)
|
|
(skip-chars-backward "\n\r\t "))
|
|
|
|
(defun org-footnote-delete (&optional label)
|
|
"Delete the footnote at point.
|
|
This will remove the definition (even multiple definitions if they exist)
|
|
and all references of a footnote label."
|
|
(catch 'done
|
|
(let (x label l beg def-re (nref 0) (ndef 0))
|
|
(unless label
|
|
(when (setq x (org-footnote-at-reference-p))
|
|
(setq label (nth 1 x))
|
|
(when (or (not label) (equal "fn:" label))
|
|
(delete-region (1+ (match-beginning 0)) (match-end 0))
|
|
(message "Anonymous footnote removed")
|
|
(throw 'done t)))
|
|
(when (and (not label) (setq x (org-footnote-at-definition-p)))
|
|
(setq label (nth 1 x)))
|
|
(unless label (error "Don't know which footnote to remove")))
|
|
(save-excursion
|
|
(save-restriction
|
|
(goto-char (point-min))
|
|
(while (re-search-forward org-footnote-re nil t)
|
|
(setq l (or (match-string 1) (match-string 2)))
|
|
(when (equal l label)
|
|
(delete-region (1+ (match-beginning 0)) (match-end 0))
|
|
(incf nref)))
|
|
(goto-char (point-min))
|
|
(setq def-re (concat "^\\[" (regexp-quote label) "\\]"))
|
|
(while (re-search-forward def-re nil t)
|
|
(setq beg (match-beginning 0))
|
|
(if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t)
|
|
(goto-char (match-beginning 0))
|
|
(goto-char (point-max)))
|
|
(delete-region beg (point))
|
|
(incf ndef))))
|
|
(org-footnote-auto-adjust-maybe)
|
|
(message "%d definition(s) of and %d reference(s) of footnote %s removed"
|
|
ndef nref label))))
|
|
|
|
(defun org-footnote-renumber-fn:N ()
|
|
"Renumber the simple footnotes like fn:17 into a sequence in the document."
|
|
(interactive)
|
|
(let (map i (n 0))
|
|
(save-excursion
|
|
(save-restriction
|
|
(widen)
|
|
(goto-char (point-min))
|
|
(while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t)
|
|
(setq i (string-to-number (match-string 1)))
|
|
(when (and (string-match "\\S-" (buffer-substring
|
|
(point-at-bol) (match-beginning 0)))
|
|
(not (assq i map)))
|
|
(push (cons i (number-to-string (incf n))) map)))
|
|
(goto-char (point-min))
|
|
(while (re-search-forward "\\(\\[fn:\\)\\([0-9]+\\)\\([]:]\\)" nil t)
|
|
(replace-match (concat "\\1" (cdr (assq (string-to-number (match-string 2)) map)) "\\3")))))))
|
|
|
|
(defun org-footnote-auto-adjust-maybe ()
|
|
"Renumber and/or sort footnotes according to user settings."
|
|
(when (memq org-footnote-auto-adjust '(t renumber))
|
|
(org-footnote-renumber-fn:N))
|
|
(when (memq org-footnote-auto-adjust '(t sort))
|
|
(let ((label (nth 1 (org-footnote-at-definition-p))))
|
|
(org-footnote-normalize 'sort)
|
|
(when label
|
|
(goto-char (point-min))
|
|
(and (re-search-forward (concat "^\\[" (regexp-quote label) "\\]")
|
|
nil t)
|
|
(progn (insert " ")
|
|
(just-one-space)))))))
|
|
|
|
(provide 'org-footnote)
|
|
|
|
;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
|
|
|
|
;;; org-footnote.el ends here
|