From b6dbf8881076191e1351d7cd15e26547a2531fea Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sun, 13 Oct 2024 10:51:50 +0200 Subject: [PATCH] org-footnote-new: Do not throw an error at bol for inline/anonymous footnotes * lisp/org-footnote.el (org-footnote--allow-reference-p): New optional argument indicating that the footnote reference is going to be anonymous/inline. It is then allowed to be at bol. (org-footnote-new): Check whether a footnote reference can be inserted in place taking into account the footnote type. * testing/lisp/test-org-footnote.el (test-org-footnote/new): New test cases. Reported-by: Suhail Singh Link: https://orgmode.org/list/877carzufa.fsf@gmail.com --- lisp/org-footnote.el | 19 ++++++++++++++----- testing/lisp/test-org-footnote.el | 11 +++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lisp/org-footnote.el b/lisp/org-footnote.el index b886e4c0e..64037deb2 100644 --- a/lisp/org-footnote.el +++ b/lisp/org-footnote.el @@ -248,13 +248,16 @@ otherwise." ;;;; Internal functions -(defun org-footnote--allow-reference-p () - "Non-nil when a footnote reference can be inserted at point." +(defun org-footnote--allow-reference-p (&optional inline) + "Non-nil when a footnote reference can be inserted at point. +When optional argument INLINE is non-nil, assume that the footnote +reference is an inline or anonymous footnote (and can be placed at the +beginning of the line)." ;; XXX: This is similar to `org-footnote-in-valid-context-p' but ;; more accurate and usually faster, except in some corner cases. ;; It may replace it after doing proper benchmarks as it would be ;; used in fontification. - (unless (bolp) + (unless (and (not inline) (bolp)) (let* ((context (org-element-context)) (type (org-element-type context))) (cond @@ -665,8 +668,6 @@ 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) - (unless (org-footnote--allow-reference-p) - (user-error "Cannot insert a footnote here")) (let* ((all (org-footnote-all-labels)) (label (unless (eq org-footnote-auto-label 'anonymous) @@ -680,16 +681,24 @@ or new, let the user edit the definition of the footnote." (mapcar #'list all) nil nil (and (eq org-footnote-auto-label 'confirm) propose))))))))) (cond ((not label) + (unless (org-footnote--allow-reference-p 'anonymous) + (user-error "Cannot insert a footnote here")) (insert "[fn::]") (backward-char 1)) ((member label all) + (unless (org-footnote--allow-reference-p) + (user-error "Cannot insert a footnote here")) (insert "[fn:" label "]") (message "New reference to existing note")) (org-footnote-define-inline + (unless (org-footnote--allow-reference-p 'inline) + (user-error "Cannot insert a footnote here")) (insert "[fn:" label ":]") (backward-char 1) (org-footnote-auto-adjust-maybe)) (t + (unless (org-footnote--allow-reference-p) + (user-error "Cannot insert a footnote here")) (insert "[fn:" label "]") (let ((p (org-footnote-create-definition label))) ;; `org-footnote-goto-definition' needs to be called diff --git a/testing/lisp/test-org-footnote.el b/testing/lisp/test-org-footnote.el index 90a917c6a..21f6ae884 100644 --- a/testing/lisp/test-org-footnote.el +++ b/testing/lisp/test-org-footnote.el @@ -57,6 +57,17 @@ (should-error (org-test-with-temp-text "Test" (org-footnote-new))) + ;; ... but not when inserting anonymous or inline footnote + (should + (org-test-with-temp-text "Test" + (let ((org-footnote-define-inline t)) + (org-footnote-new) + t))) + (should + (org-test-with-temp-text "Test" + (let ((org-footnote-auto-label 'anonymous)) + (org-footnote-new) + t))) ;; Error at keywords. (should-error (org-test-with-temp-text "#+TITLE: value"