From 78c450247b4a04a19519714c7175d1186bd7ea63 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 28 Jul 2021 11:11:46 +0200 Subject: [PATCH] oc: Allow inserting citations in captions * lisp/oc.el (org-cite--allowed-p): Allow inserting citations in captions. * testing/lisp/test-oc.el (test-org-cite/insert-capability): Add test. --- lisp/oc.el | 22 +++++++++++++++++++--- testing/lisp/test-oc.el | 12 +++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lisp/oc.el b/lisp/oc.el index 1d704fb03..4e21a0903 100644 --- a/lisp/oc.el +++ b/lisp/oc.el @@ -58,6 +58,7 @@ (require 'org-compat) (require 'org-macs) +(declare-function org-at-heading-p "org" (&optional _)) (declare-function org-collect-keywords "org" (keywords &optional unique directory)) (declare-function org-element-adopt-elements "org-element" (parent &rest children)) @@ -73,6 +74,7 @@ (declare-function org-element-normalize-string "org-element" (s)) (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only)) (declare-function org-element-parse-secondary-string "org-element" (string restriction &optional parent)) +(declare-function org-element-context "org-element" (&optional element)) (declare-function org-element-property "org-element" (property element)) (declare-function org-element-put-property "org-element" (element property value)) (declare-function org-element-restriction "org-element" (element)) @@ -85,9 +87,11 @@ (declare-function org-export-get-previous-element "org-export" (blob info &optional n)) (declare-function org-export-raw-string "org-export" (s)) +(defvar org-complex-heading-regexp) (defvar org-element-all-objects) (defvar org-element-citation-key-re) (defvar org-element-citation-prefix-re) +(defvar org-element-parsed-keywords) ;;; Constants @@ -1363,12 +1367,24 @@ ARG is the prefix argument received when calling `org-open-at-point', or nil." ;;; Meta-command for citation insertion (insert capability) (defun org-cite--allowed-p (context) "Non-nil when a citation can be inserted at point." - (let* ((type (org-element-type context))) + (let ((type (org-element-type context))) (cond - ;; No citation in attributes. + ;; No citation in attributes, except in parsed ones. + ;; + ;; XXX: Inserting citation in a secondary value is not allowed + ;; yet. Is it useful? ((let ((post (org-element-property :post-affiliated context))) (and post (< (point) post))) - nil) + (let ((case-fold-search t)) + (looking-back + (rx-to-string + `(seq line-start (0+ (any " \t")) + "#+" + (or ,@org-element-parsed-keywords) + ":" + (0+ nonl)) + t) + (line-beginning-position)))) ;; Paragraphs and blank lines at top of document are fine. ((memq type '(nil paragraph))) ;; So are contents of verse blocks. diff --git a/testing/lisp/test-oc.el b/testing/lisp/test-oc.el index 16e67ac73..80831f212 100644 --- a/testing/lisp/test-oc.el +++ b/testing/lisp/test-oc.el @@ -1768,7 +1768,17 @@ arguments. Replace citation with \"@\" character in the output." (org-cite-insert-processor 'foo)) (org-cite-register-processor 'foo :insert (lambda (_ _) (throw :exit 'success))) - (call-interactively #'org-cite-insert))))) + (call-interactively #'org-cite-insert)))) + ;; Allow inserting citations in captions. + (should + (eq 'success + (catch :exit + (org-test-with-temp-text "#+caption: \n| table |" + (let ((org-cite--processors nil) + (org-cite-insert-processor 'foo)) + (org-cite-register-processor 'foo + :insert (lambda (_ _) (throw :exit 'success))) + (call-interactively #'org-cite-insert))))))) (provide 'test-oc) ;;; test-oc.el ends here