org-footnote: Do not move point if definition is unreachable

* lisp/org-footnote.el (org-footnote-goto-definition): Throw an error
  when definition is outside narrowed part of buffer.  Do not move
  point either.

* testing/lisp/test-org-footnote.el (test-org-footnote/goto-definition):
  New test.

Reported-by: Rasmus <rasmus@gmx.us>
<http://permalink.gmane.org/gmane.emacs.orgmode/97158>
This commit is contained in:
Nicolas Goaziou 2015-04-25 10:36:35 +02:00
parent 0d21d8d0ea
commit 5954f6aa25
2 changed files with 35 additions and 5 deletions

View File

@ -360,17 +360,20 @@ If no footnote is found, return nil."
"Move point to the definition of the footnote LABEL.
Return a non-nil value when a definition has been found."
(interactive "sLabel: ")
(org-mark-ring-push)
(let ((def (org-footnote-get-definition label)))
(if (not def)
(error "Cannot find definition of footnote %s" label)
(cond
((not def) (user-error "Cannot find definition of footnote %s" label))
((> (nth 1 def) (point-max))
(user-error "Footnote definition outside of narrowed part of buffer"))
(t
(org-mark-ring-push)
(goto-char (nth 1 def))
(looking-at (format "\\[%s\\]\\|\\[%s:" label label))
(looking-at (format "\\[%s[]:]" label))
(goto-char (match-end 0))
(org-show-context 'link-search)
(when (derived-mode-p 'org-mode)
(message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))
t)))
t))))
(defun org-footnote-goto-previous-reference (label)
"Find the first closest (to point) reference of footnote with label LABEL."

View File

@ -152,6 +152,33 @@
(org-footnote-delete "1")
(org-trim (buffer-string))))))
(ert-deftest test-org-footnote/goto-definition ()
"Test `org-footnote-goto-definition' specifications."
;; Error on unknown definitions.
(should-error
(org-test-with-temp-text "No footnote definition"
(org-footnote-goto-definition "fn:1")))
;; Error when trying to reach a definition outside narrowed part of
;; buffer.
(should-error
(org-test-with-temp-text "Some text<point>\n[fn:1] Definition."
(narrow-to-region (point-min) (point))
(org-footnote-goto-definition "fn:1")))
;; Otherwise, move at the beginning of the definition, including
;; anonymous footnotes.
(should
(equal
" Definition."
(org-test-with-temp-text "Some text\n[fn:1] Definition."
(org-footnote-goto-definition "fn:1")
(buffer-substring (point) (point-max)))))
(should
(equal
"definition]"
(org-test-with-temp-text "Some text[fn:label:definition]"
(org-footnote-goto-definition "fn:label")
(buffer-substring (point) (point-max))))))
(ert-deftest test-org-footnote/normalize-in-org ()
"Test specifications for `org-footnote-normalize' in an Org buffer."
;; 1. With a non-nil `org-footnote-section'.