diff --git a/lisp/org-footnote.el b/lisp/org-footnote.el index 2b04e00c6..f40f36fce 100644 --- a/lisp/org-footnote.el +++ b/lisp/org-footnote.el @@ -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." diff --git a/testing/lisp/test-org-footnote.el b/testing/lisp/test-org-footnote.el index 1a10faa21..b778c2ee5 100644 --- a/testing/lisp/test-org-footnote.el +++ b/testing/lisp/test-org-footnote.el @@ -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\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'.