oc.el 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. ;;; oc.el --- Org Cite library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library provides tooling to handle citations in Org, e.g,
  17. ;; activate, follow, insert, and export them, respectively called
  18. ;; "activate", "follow", "insert" and "export" capabilities.
  19. ;; Libraries responsible for providing some, or all, of these
  20. ;; capabilities are called "citation processors".
  21. ;; Such processors are defined using `org-cite-register-processor'.
  22. ;; Using this function, it is possible, in addition to giving it a
  23. ;; name, to attach functions associated to capabilities. As such, a
  24. ;; processor handling citation export must set the `:export-citation'
  25. ;; property to an appropriate function. Likewise, "activate"
  26. ;; capability requires an appropriate `:activate' property, "insert"
  27. ;; requires `:insert' property and, unsurprisingly, "follow"
  28. ;; capability implies `:follow' property.
  29. ;; As a user, the first thing to do is setting a bibliography, either
  30. ;; globally with `org-cite-global-bibliography', or locally using one
  31. ;; or more "bibliography" keywords. Then one can select any
  32. ;; registered processor for each capability by providing a processor
  33. ;; name to the variables `org-cite-activate-processor' and
  34. ;; `org-cite-follow-processor'.
  35. ;; The "export" capability is slightly more involved as one need to
  36. ;; select the processor providing it, but may also provide a default
  37. ;; style for citations and bibliography. Also, the choice of an
  38. ;; export processor may depend of the current export back-end. The
  39. ;; association between export back-ends and triplets of parameters can
  40. ;; be set in `org-cite-export-processors' variable, or in a document,
  41. ;; through the "cite_export" keyword.
  42. ;; Eventually, this library provides some tools, mainly targeted at
  43. ;; processor implementors. Most are export-specific and are located
  44. ;; in the "Tools only available during export" and "Tools generating
  45. ;; or operating on parsed data" sections.
  46. ;; The few others can be used directly from an Org buffer, or operate
  47. ;; on processors. See "Generic tools" section.
  48. ;;; Code:
  49. (require 'org-compat)
  50. (require 'org-macs)
  51. (require 'seq)
  52. (declare-function org-at-heading-p "org" (&optional _))
  53. (declare-function org-collect-keywords "org" (keywords &optional unique directory))
  54. (declare-function org-element-adopt-elements "org-element" (parent &rest children))
  55. (declare-function org-element-citation-parser "org-element" ())
  56. (declare-function org-element-citation-reference-parser "org-element" ())
  57. (declare-function org-element-class "org-element" (datum &optional parent))
  58. (declare-function org-element-contents "org-element" (element))
  59. (declare-function org-element-create "org-element" (type &optional props &rest children))
  60. (declare-function org-element-extract-element "org-element" (element))
  61. (declare-function org-element-insert-before "org-element" (element location))
  62. (declare-function org-element-lineage "org-element" (datum &optional types with-self))
  63. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
  64. (declare-function org-element-normalize-string "org-element" (s))
  65. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  66. (declare-function org-element-parse-secondary-string "org-element" (string restriction &optional parent))
  67. (declare-function org-element-context "org-element" (&optional element))
  68. (declare-function org-element-property "org-element" (property element))
  69. (declare-function org-element-put-property "org-element" (element property value))
  70. (declare-function org-element-restriction "org-element" (element))
  71. (declare-function org-element-set-element "org-element" (old new))
  72. (declare-function org-element-type "org-element" (element))
  73. (declare-function org-export-derived-backend-p "org-export" (backend &rest backends))
  74. (declare-function org-export-get-next-element "org-export" (blob info &optional n))
  75. (declare-function org-export-get-previous-element "org-export" (blob info &optional n))
  76. (declare-function org-export-raw-string "org-export" (s))
  77. (defvar org-complex-heading-regexp)
  78. (defvar org-element-all-objects)
  79. (defvar org-element-citation-key-re)
  80. (defvar org-element-citation-prefix-re)
  81. (defvar org-element-parsed-keywords)
  82. ;;; Constants
  83. ;; Borrowed from "citeproc.el" library.
  84. (defconst org-cite--default-region-alist
  85. '(("af" . "za") ("ca" . "ad") ("cs" . "cz") ("cy" . "gb")
  86. ("da" . "dk") ("el" . "gr") ("et" . "ee") ("fa" . "ir")
  87. ("he" . "ir") ("ja" . "jp") ("km" . "kh") ("ko" . "kr")
  88. ("nb" . "no") ("nn" . "no") ("sl" . "si") ("sr" . "rs")
  89. ("sv" . "se") ("uk" . "ua") ("vi" . "vn") ("zh" . "cn"))
  90. "Alist mapping those languages to their default region.
  91. Only those languages are given for which the default region is not simply the
  92. result of duplicating the language part.")
  93. ;;; Configuration variables
  94. (defgroup org-cite nil
  95. "Options concerning citations in Org mode."
  96. :group 'org
  97. :tag "Org Cite")
  98. (defcustom org-cite-global-bibliography nil
  99. "List of bibliography files available in all documents.
  100. File names must be absolute."
  101. :group 'org-cite
  102. :package-version '(Org . "9.5")
  103. :type '(choice (const :tag "No global bibliography" nil)
  104. (repeat :tag "List of bibliography files"
  105. (file :tag "Bibliography"))))
  106. (defcustom org-cite-activate-processor 'basic
  107. "Processor used for activating citations, as a symbol."
  108. :group 'org-cite
  109. :package-version '(Org . "9.5")
  110. :type '(choice (const :tag "Default fontification" nil)
  111. (symbol :tag "Citation processor")))
  112. (defcustom org-cite-export-processors '((t basic))
  113. "Processor used for exporting citations, as a triplet, or nil.
  114. When nil, citations and bibliography are not exported.
  115. When non-nil, the value is an association list between export back-ends and
  116. citation export processors:
  117. (BACK-END . PROCESSOR)
  118. where BACK-END is the name of an export back-end or t, and PROCESSOR is a
  119. triplet following the pattern
  120. (NAME BIBLIOGRAPHY-STYLE CITATION-STYLE)
  121. There, NAME is the name of a registered citation processor providing export
  122. functionality, as a symbol. BIBLIOGRAPHY-STYLE (respectively CITATION-STYLE)
  123. is the desired default style to use when printing a bibliography (respectively
  124. exporting a citation), as a string or nil. Both BIBLIOGRAPHY-STYLE and
  125. CITATION-STYLE are optional. NAME is mandatory.
  126. The export process selects the citation processor associated to the current
  127. export back-end, or the most specific back-end the current one is derived from,
  128. or, if all are inadequate, to the processor associated to t. For example, with
  129. the following value
  130. ((beamer natbib)
  131. (latex biblatex)
  132. (t csl))
  133. exporting with `beamer' or any back-end derived from it will use `natbib',
  134. whereas exporting with `latex' or any back-end derived from it but different
  135. from `beamer' will use `biblatex' processor. Any other back-end, such as
  136. `html', will use `csl' processor.
  137. CITATION-STYLE is overridden by adding a style to any citation object. A nil
  138. style lets the export processor choose the default output. Any style not
  139. recognized by the export processor is equivalent to nil.
  140. The citation triplet can also be set with the CITE_EXPORT keyword.
  141. E.g.,
  142. #+CITE_EXPORT: basic note numeric
  143. or
  144. #+CITE_EXPORT: basic
  145. In that case, `basic' processor is used on every export, independently on the
  146. back-end."
  147. :group 'org-cite
  148. :package-version '(Org . "9.5")
  149. :type '(choice (const :tag "No export" nil)
  150. (alist :key-type symbol
  151. :value-type
  152. (list :tag "Citation processor"
  153. (symbol :tag "Processor name")
  154. (choice
  155. (const :tag "Default bibliography style" nil)
  156. (string :tag "Use specific bibliography style"))
  157. (choice
  158. (const :tag "Default citation style" nil)
  159. (string :tag "Use specific citation style"))))))
  160. (defcustom org-cite-follow-processor 'basic
  161. "Processor used for following citations, as a symbol."
  162. :group 'org-cite
  163. :package-version '(Org . "9.5")
  164. :type '(choice (const :tag "No following" nil)
  165. (symbol :tag "Citation processor")))
  166. (defcustom org-cite-insert-processor 'basic
  167. "Processor used for inserting citations, as a symbol."
  168. :group 'org-cite
  169. :package-version '(Org . "9.5")
  170. :type '(choice (const :tag "No insertion" nil)
  171. (symbol :tag "Citation processor")))
  172. (defcustom org-cite-adjust-note-numbers t
  173. "When non-nil, allow process to modify location of note numbers.
  174. When this variable is non-nil, it is possible to swap between author-date and
  175. note style without modifying the document. To that effect, citations should
  176. always be located as in an author-date style. Prior to turning the citation
  177. into a footnote, the citation processor moves the citation (i.e., the future
  178. note number), and the surrounding punctuation, according to rules defined in
  179. `org-cite-note-rules'.
  180. When nil, the note number is not moved."
  181. :group 'org-cite
  182. :package-version '(Org . "9.5")
  183. :type '(choice (const :tag "Automatic note number location" t)
  184. (const :tag "Place note numbers manually" nil))
  185. :safe #'booleanp)
  186. (defcustom org-cite-note-rules
  187. '(("en-us" inside outside after)
  188. ("fr" adaptive same before))
  189. "Alist between languages and typographic rules for citations in note style.
  190. When `org-cite-adjust-note-numbers' is non-nil, and note style is requested,
  191. citation processor is allowed to move the note marker according to some specific
  192. rules, detailed here. More accurately, a rule is a list following the pattern
  193. (LANGUAGE-TAG . RULE)
  194. LANGUAGE-TAG is a down-cased string representing a language tag as defined in
  195. RFC 4646. It may constituted of a language and a region separated with an
  196. hyphen (e.g., \"en-us\"), or the language alone (e.g., \"fr\"). A language
  197. without a region applies to all regions.
  198. RULE is a triplet
  199. (PUNCTUATION NUMBER ORDER)
  200. PUNCTUATION is the desired location of the punctuation with regards to the
  201. quotation, if any. It may be `inside', `outside', or `adaptive'. The latter
  202. permits subtler control over the punctuation: when there is no space between
  203. the quotation mark and the punctuation, it is equivalent to `inside'.
  204. Otherwise, it means `outside', as illustrated in the following examples:
  205. \"A quotation ending without punctuation\" [cite:@org21].
  206. \"A quotation ending with a period\"[cite:@org21].
  207. Notwithstanding the above, a space always appear before the citation when it
  208. is to become anything else than a note.
  209. NUMBER is the desired location of the note number with regards to the
  210. quotation mark, if any. It may be `inside', `outside', or `same'. When set
  211. to `same', the number appears on the same side as the punctuation, unless
  212. there is punctuation on both sides or on none.
  213. ORDER is the relative position of the citation with regards to the closest
  214. punctuation. It may be `after' or `before'.
  215. For example (adaptive same before) corresponds to French typography.
  216. When the locale is unknown to this variable, the default rule is:
  217. (adaptive outside after)
  218. This roughly follows the Oxford Guide to Style recommendations."
  219. :group 'org-cite
  220. :package-version '(Org . "9.5")
  221. :type
  222. '(repeat
  223. (list :tag "Typographic rule"
  224. (string :tag "Language code")
  225. (choice :tag "Location of punctuation"
  226. (const :tag "Punctuation inside quotation" inside)
  227. (const :tag "Punctuation outside quotation" outside)
  228. (const :tag "Location depends on spacing" adaptive))
  229. (choice :tag "Location of citation"
  230. (const :tag "Citation inside quotation" inside)
  231. (const :tag "Citation outside quotation" outside)
  232. (const :tag "Citation next to punctuation" same))
  233. (choice :tag "Order of citation and punctuation"
  234. (const :tag "Citation first" before)
  235. (const :tag "Citation last" after)))))
  236. (defcustom org-cite-punctuation-marks '("." "," ";" ":" "!" "?")
  237. "List of strings that can be moved around when placing note numbers.
  238. When `org-cite-adjust-note-numbers' is non-nil, the citation processor is
  239. allowed to shuffle punctuation marks specified in this list in order to
  240. place note numbers according to rules defined in `org-cite-note-rules'."
  241. :group 'org-cite
  242. :package-version '(Org . "9.5")
  243. :type '(repeat string))
  244. ;;; Citation processors
  245. (cl-defstruct (org-cite-processor (:constructor org-cite--make-processor)
  246. (:copier nil))
  247. (name nil :read-only t)
  248. (activate nil :read-only t)
  249. (cite-styles nil :read-only t)
  250. (export-bibliography nil :read-only t)
  251. (export-citation nil :read-only t)
  252. (export-finalizer nil :read-only t)
  253. (follow nil :read-only t)
  254. (insert nil :read-only t))
  255. (defvar org-cite--processors nil
  256. "List of registered citation processors.
  257. See `org-cite-register-processor' for more information about
  258. processors.")
  259. (defun org-cite-register-processor (name &rest body)
  260. "Mark citation processor NAME as available.
  261. NAME is a symbol. BODY is a property list, where the following
  262. optional keys can be set:
  263. `:activate'
  264. Function activating a citation. It is called with a single
  265. argument: a citation object extracted from the current
  266. buffer. It may add text properties to the buffer. If it is
  267. not provided, `org-cite-fontify-default' is used.
  268. `:export-bibliography'
  269. Function rendering a bibliography. It is called with six
  270. arguments: the list of citation keys used in the document, as
  271. strings, a list of bibliography files, the style, as a string
  272. or nil, the local properties, as a property list, the export
  273. back-end, as a symbol, and the communication channel, as a
  274. property list.
  275. It is called at each \"print_bibliography\" keyword in the
  276. parse tree. It may return a string, a parsed element, a list
  277. of parsed elements, or nil. When it returns nil, the keyword
  278. is ignored. Otherwise, the value it returns replaces the
  279. keyword in the export output.
  280. `:export-citation' (mandatory for \"export\" capability)
  281. Function rendering citations. It is called with four
  282. arguments: a citation object, the style, as a pair, the
  283. export back-end, as a symbol, and the communication channel,
  284. as a property list.
  285. It is called on each citation object in the parse tree. It
  286. may return a string, a parsed object, a secondary string, or
  287. nil. When it returns nil, the citation is ignored.
  288. Otherwise, the value it returns replaces the citation object
  289. in the export output.
  290. `:export-finalizer'
  291. Function called at the end of export process. It must accept
  292. six arguments: the output, as a string, a list of citation
  293. keys used in the document, a list of bibliography files, the
  294. expected bibliography style, as a string or nil, the export
  295. back-end, as a symbol, and the communication channel, as a
  296. property list.
  297. It must return a string, which will become the final output
  298. from the export process, barring subsequent modifications
  299. from export filters.
  300. `:follow'
  301. Function called to follow a citation. It accepts two
  302. arguments, the citation or citation reference object at
  303. point, and any prefix argument received during interactive
  304. call of `org-open-at-point'.
  305. `:insert'
  306. Function called to insert a citation. It accepts two
  307. arguments, the citation or citation reference object at point
  308. or nil, and any prefix argument received.
  309. `:cite-styles'
  310. When the processor has export capability, the value can
  311. specify what cite styles, variants, and their associated
  312. shortcuts are supported. It can be useful information for
  313. completion or linting.
  314. The expected format is
  315. ((STYLE . SHORTCUTS) . VARIANTS))
  316. where STYLE is a string, SHORTCUTS a list of strings or nil,
  317. and VARIANTS is a list of pairs (VARIANT . SHORTCUTS),
  318. VARIANT being a string and SHORTCUTS a list of strings or
  319. nil.
  320. The \"nil\" style denotes the processor fall-back style. It
  321. should have a corresponding entry in the value.
  322. The value can also be a function. It will be called without
  323. any argument and should return a list structured as the above.
  324. Return a non-nil value on a successful operation."
  325. (declare (indent 1))
  326. (unless (and name (symbolp name))
  327. (error "Invalid processor name: %S" name))
  328. (setq org-cite--processors
  329. (cons (apply #'org-cite--make-processor :name name body)
  330. (seq-remove (lambda (p) (eq name (org-cite-processor-name p)))
  331. org-cite--processors))))
  332. (defun org-cite-try-load-processor (name)
  333. "Try loading citation processor NAME if unavailable.
  334. NAME is a symbol. When the NAME processor is unregistered, try
  335. loading \"oc-NAME\" library beforehand, then cross fingers."
  336. (unless (org-cite-get-processor name)
  337. (require (intern (format "oc-%s" name)) nil t)))
  338. (defun org-cite-get-processor (name)
  339. "Return citation processor named after symbol NAME.
  340. Return nil if no such processor is found."
  341. (seq-find (lambda (p) (eq name (org-cite-processor-name p)))
  342. org-cite--processors))
  343. (defun org-cite-unregister-processor (name)
  344. "Unregister citation processor NAME.
  345. NAME is a symbol. Raise an error if processor is not registered.
  346. Return a non-nil value on a successful operation."
  347. (unless (and name (symbolp name))
  348. (error "Invalid processor name: %S" name))
  349. (pcase (org-cite-get-processor name)
  350. ('nil (error "Processor %S not registered" name))
  351. (processor
  352. (setq org-cite--processors (delete processor org-cite--processors))))
  353. t)
  354. (defun org-cite-processor-has-capability-p (processor capability)
  355. "Return non-nil if PROCESSOR is able to handle CAPABILITY.
  356. PROCESSOR is the name of a cite processor, as a symbol. CAPABILITY is
  357. `activate', `export', `follow', or `insert'."
  358. (let ((p (org-cite-get-processor processor)))
  359. (pcase capability
  360. ((guard (not p)) nil) ;undefined processor
  361. ('activate (functionp (org-cite-processor-activate p)))
  362. ('export (functionp (org-cite-processor-export-citation p)))
  363. ('follow (functionp (org-cite-processor-follow p)))
  364. ('insert (functionp (org-cite-processor-insert p)))
  365. (other (error "Invalid capability: %S" other)))))
  366. ;;; Internal functions
  367. (defun org-cite--set-post-blank (datum blanks)
  368. "Set `:post-blank' property from element or object before DATUM to BLANKS.
  369. DATUM is an element or object. BLANKS is an integer. DATUM is modified
  370. by side-effect."
  371. (if (not (eq 'plain-text (org-element-type datum)))
  372. (org-element-put-property datum :post-blank blanks)
  373. ;; Remove any blank from string before DATUM so it is exported
  374. ;; with exactly BLANKS white spaces.
  375. (org-element-set-element
  376. datum
  377. (replace-regexp-in-string
  378. "[ \t\n]*\\'" (make-string blanks ?\s) datum))))
  379. (defun org-cite--set-previous-post-blank (datum blanks info)
  380. "Set `:post-blank' property from element or object before DATUM to BLANKS.
  381. DATUM is an element or object. BLANKS is an integer. INFO is the export
  382. state, as a property list. Previous element or object, if any, is modified by
  383. side-effect."
  384. (let ((previous (org-export-get-previous-element datum info)))
  385. (when previous
  386. (org-cite--set-post-blank previous blanks))))
  387. (defun org-cite--insert-at-split (s citation n regexp)
  388. "Split string S and insert CITATION object between the two parts.
  389. S is split at beginning of match group N upon matching REGEXP against it.
  390. This function assumes S precedes CITATION."
  391. ;; When extracting the citation, remove white spaces before it, but
  392. ;; preserve those after it.
  393. (let ((post-blank (org-element-property :post-blank citation)))
  394. (when (and post-blank (> post-blank 0))
  395. (org-element-insert-before (make-string post-blank ?\s) citation)))
  396. (org-element-insert-before
  397. (org-element-put-property (org-element-extract-element citation)
  398. :post-blank 0)
  399. s)
  400. (string-match regexp s)
  401. (let* ((split (match-beginning n))
  402. (first-part (substring s nil split))
  403. ;; Remove trailing white spaces as they are before the
  404. ;; citation.
  405. (last-part
  406. (replace-regexp-in-string (rx (1+ (any blank ?\n)) string-end)
  407. ""
  408. (substring s split))))
  409. (when (org-string-nw-p first-part)
  410. (org-element-insert-before first-part citation))
  411. (org-element-set-element s last-part)))
  412. (defun org-cite--move-punct-before (punct citation s info)
  413. "Move punctuation PUNCT before CITATION object.
  414. String S contains PUNCT. INFO is the export state, as a property list.
  415. The function assumes S follows CITATION. Parse tree is modified by side-effect."
  416. (if (equal s punct)
  417. (org-element-extract-element s) ;it would be empty anyway
  418. (org-element-set-element s (substring s (length punct))))
  419. ;; Remove blanks before citation.
  420. (org-cite--set-previous-post-blank citation 0 info)
  421. (org-element-insert-before
  422. ;; Blanks between citation and punct are now before punct and
  423. ;; citation.
  424. (concat (make-string (or (org-element-property :post-blank citation) 0) ?\s)
  425. punct)
  426. citation))
  427. (defun org-cite--parse-as-plist (s)
  428. "Parse string S as a property list.
  429. Values are always strings. Return nil if S is nil."
  430. (cond
  431. ((null s) nil)
  432. ((stringp s)
  433. (with-temp-buffer
  434. (save-excursion (insert s))
  435. (skip-chars-forward " \t")
  436. (let ((results nil)
  437. (value-flag nil))
  438. (while (not (eobp))
  439. (pcase (char-after)
  440. (?:
  441. (push (read (current-buffer)) results)
  442. (setq value-flag t))
  443. ((guard (not value-flag))
  444. (skip-chars-forward "^ \t"))
  445. (?\"
  446. (let ((origin (point)))
  447. (condition-case _
  448. (progn
  449. (read (current-buffer))
  450. (push (buffer-substring (1+ origin) (1- (point))) results))
  451. (end-of-file
  452. (goto-char origin)
  453. (skip-chars-forward "^ \t")
  454. (push (buffer-substring origin (point)) results)))
  455. (setq value-flag nil)))
  456. (_
  457. (let ((origin (point)))
  458. (skip-chars-forward "^ \t")
  459. (push (buffer-substring origin (point)) results)
  460. (setq value-flag nil))))
  461. (skip-chars-forward " \t"))
  462. (nreverse results))))
  463. (t (error "Invalid argument type: %S" s))))
  464. (defun org-cite--get-note-rule (info)
  465. "Return punctuation rule according to language used for export.
  466. INFO is the export state, as a property list.
  467. Rule is found according to the language used for export and
  468. `org-cite-note-rules', which see.
  469. If there is no rule matching current language, the rule defaults
  470. to (adaptive outside after)."
  471. (let* ((language-tags
  472. ;; Normalize language as a language-region tag, as described
  473. ;; in RFC 4646.
  474. (pcase (split-string (plist-get info :language) "[-_]")
  475. (`(,language)
  476. (list language
  477. (or (cdr (assoc language org-cite--default-region-alist))
  478. language)))
  479. (`(,language ,region)
  480. (list language region))
  481. (other
  482. (error "Invalid language identifier: %S" other))))
  483. (language-region (mapconcat #'downcase language-tags "-"))
  484. (language (car language-tags)))
  485. (or (cdr (assoc language-region org-cite-note-rules))
  486. (cdr (assoc language org-cite-note-rules))
  487. '(adaptive outside after))))
  488. ;;; Generic tools
  489. (defun org-cite-list-bibliography-files ()
  490. "List all bibliography files defined in the buffer."
  491. (delete-dups
  492. (append (mapcar (lambda (value)
  493. (pcase value
  494. (`(,f . ,d)
  495. (expand-file-name (org-strip-quotes f) d))))
  496. (pcase (org-collect-keywords
  497. '("BIBLIOGRAPHY") nil '("BIBLIOGRAPHY"))
  498. (`(("BIBLIOGRAPHY" . ,pairs)) pairs)))
  499. org-cite-global-bibliography)))
  500. (defun org-cite-get-references (citation &optional keys-only)
  501. "Return citations references contained in CITATION object.
  502. When optional argument KEYS-ONLY is non-nil, return the references' keys, as a
  503. list of strings.
  504. Assume CITATION object comes from either a full parse tree, e.g., during export,
  505. or from the current buffer."
  506. (let ((contents (org-element-contents citation)))
  507. (cond
  508. ((null contents)
  509. (org-with-point-at (org-element-property :contents-begin citation)
  510. (narrow-to-region (point) (org-element-property :contents-end citation))
  511. (let ((references nil))
  512. (while (not (eobp))
  513. (let ((reference (org-element-citation-reference-parser)))
  514. (goto-char (org-element-property :end reference))
  515. (push (if keys-only
  516. (org-element-property :key reference)
  517. reference)
  518. references)))
  519. (nreverse references))))
  520. (keys-only (mapcar (lambda (r) (org-element-property :key r)) contents))
  521. (t contents))))
  522. (defun org-cite-boundaries (citation)
  523. "Return the beginning and end strict position of CITATION.
  524. Returns a (BEG . END) pair."
  525. (let ((beg (org-element-property :begin citation))
  526. (end (org-with-point-at (org-element-property :end citation)
  527. (skip-chars-backward " \t")
  528. (point))))
  529. (cons beg end)))
  530. (defun org-cite-key-boundaries (reference)
  531. "Return citation REFERENCE's key boundaries as buffer positions.
  532. The function returns a pair (START . END) where START and END denote positions
  533. in the current buffer. Positions include leading \"@\" character."
  534. (org-with-point-at (org-element-property :begin reference)
  535. (let ((end (org-element-property :end reference)))
  536. (re-search-forward org-element-citation-key-re end t)
  537. (cons (match-beginning 0) (match-end 0)))))
  538. (defun org-cite-main-affixes (citation)
  539. "Return main affixes for CITATION object.
  540. Some export back-ends only support a single pair of affixes per
  541. citation, even if it contains multiple keys. This function
  542. decides what affixes are the most appropriate.
  543. Return a pair (PREFIX . SUFFIX) where PREFIX and SUFFIX are
  544. parsed data."
  545. (let ((source
  546. ;; When there are multiple references, use global affixes.
  547. ;; Otherwise, local affixes have priority.
  548. (pcase (org-cite-get-references citation)
  549. (`(,reference) reference)
  550. (_ citation))))
  551. (cons (org-element-property :prefix source)
  552. (org-element-property :suffix source))))
  553. (defun org-cite-supported-styles (&optional processors)
  554. "List of supported citation styles and variants.
  555. Supported styles are those handled by export processors from
  556. `org-cite-export-processors', or in PROCESSORS, as a list of symbols,
  557. when non-nil.
  558. Return value is a list with the following items:
  559. ((STYLE . SHORTCUTS) . VARIANTS))
  560. where STYLE is a string, SHORTCUTS a list of strings, and VARIANTS is a list of
  561. pairs (VARIANT . SHORTCUTS), VARIANT being a string and SHORTCUTS a list of
  562. strings."
  563. (let ((collection
  564. (seq-mapcat
  565. (lambda (name)
  566. (pcase (org-cite-processor-cite-styles
  567. (org-cite-get-processor name))
  568. ((and (pred functionp) f) (funcall f))
  569. (static-data static-data)))
  570. (or processors
  571. (mapcar (pcase-lambda (`(,_ . (,name . ,_))) name)
  572. org-cite-export-processors))))
  573. (result nil))
  574. ;; Merge duplicate styles. Each style full name is guaranteed to
  575. ;; be unique, and associated to all shortcuts and all variants in
  576. ;; the initial collection.
  577. (pcase-dolist (`((,style . ,shortcuts) . ,variants) collection)
  578. (let ((entry (assoc style result)))
  579. (if (not entry)
  580. (push (list style shortcuts variants) result)
  581. (setf (nth 1 entry)
  582. (seq-uniq (append shortcuts (nth 1 entry))))
  583. (setf (nth 2 entry)
  584. (append variants (nth 2 entry))))))
  585. ;; Return value with the desired format.
  586. (nreverse
  587. (mapcar (pcase-lambda (`(,style ,shortcuts ,variants))
  588. (cons (cons style (nreverse shortcuts))
  589. ;; Merge variant shortcuts.
  590. (let ((result nil))
  591. (pcase-dolist (`(,variant . ,shortcuts) variants)
  592. (let ((entry (assoc variant result)))
  593. (if (not entry)
  594. (push (cons variant shortcuts) result)
  595. (setf (cdr entry)
  596. (seq-uniq (append shortcuts (cdr entry)))))))
  597. result)))
  598. result))))
  599. (defun org-cite-delete-citation (datum)
  600. "Delete citation or citation reference DATUM.
  601. When removing the last reference, also remove the whole citation."
  602. (pcase (org-element-type datum)
  603. ('citation
  604. (pcase-let* ((`(,begin . ,end) (org-cite-boundaries datum))
  605. (pos-before-blank
  606. (org-with-point-at begin
  607. (skip-chars-backward " \t")
  608. (point)))
  609. (pos-after-blank (org-element-property :end datum))
  610. (first-on-line?
  611. (= pos-before-blank (line-beginning-position)))
  612. (last-on-line?
  613. (= pos-after-blank (line-end-position))))
  614. (cond
  615. ;; The citation is alone on its line. Remove the whole line.
  616. ;; Do not leave it blank as it might break a surrounding
  617. ;; paragraph.
  618. ((and first-on-line? last-on-line?)
  619. (delete-region (line-beginning-position) (line-beginning-position 2)))
  620. ;; When the citation starts the line, preserve indentation.
  621. (first-on-line? (delete-region begin pos-after-blank))
  622. ;; When the citation ends the line, remove any trailing space.
  623. (last-on-line? (delete-region pos-before-blank (line-end-position)))
  624. ;; Otherwise, delete blanks before the citation.
  625. ;; Nevertheless, make sure there is at least one blank left,
  626. ;; so as to not splice unrelated surroundings.
  627. (t
  628. (delete-region pos-before-blank end)
  629. (when (= pos-after-blank end)
  630. (org-with-point-at pos-before-blank (insert " ")))))))
  631. ('citation-reference
  632. (let* ((citation (org-element-property :parent datum))
  633. (references (org-cite-get-references citation))
  634. (begin (org-element-property :begin datum))
  635. (end (org-element-property :end datum)))
  636. (cond
  637. ;; Single reference.
  638. ((= 1 (length references))
  639. (org-cite-delete-citation citation))
  640. ;; First reference, no prefix.
  641. ((and (= begin (org-element-property :contents-begin citation))
  642. (not (org-element-property :prefix citation)))
  643. (org-with-point-at (org-element-property :begin datum)
  644. (skip-chars-backward " \t")
  645. (delete-region (point) end)))
  646. ;; Last reference, no suffix.
  647. ((and (= end (org-element-property :contents-end citation))
  648. (not (org-element-property :suffix citation)))
  649. (delete-region (1- begin) (1- (cdr (org-cite-boundaries citation)))))
  650. ;; Somewhere in-between.
  651. (t
  652. (delete-region begin end)))))
  653. (other
  654. (error "Invalid object type: %S" other))))
  655. ;;; Tools only available during export
  656. (defun org-cite-citation-style (citation info)
  657. "Return citation style used for CITATION object.
  658. Style is a pair (NAME . VARIANT) where NAME and VARIANT are strings or nil.
  659. A nil NAME means the default style for the current processor should be used.
  660. INFO is a plist used as a communication channel."
  661. (let* ((separate
  662. (lambda (s)
  663. (cond
  664. ((null s) (cons nil nil))
  665. ((not (string-match "/" s)) (cons s nil))
  666. (t (cons (substring s nil (match-beginning 0))
  667. (org-string-nw-p (substring s (match-end 0))))))))
  668. (local (funcall separate (org-element-property :style citation)))
  669. (global
  670. (funcall separate (pcase (plist-get info :cite-export)
  671. (`(,_ ,_ ,style) style)
  672. (_ nil)))))
  673. (cond
  674. ((org-string-nw-p (car local))
  675. (cons (org-not-nil (car local)) (cdr local)))
  676. (t
  677. (cons (org-not-nil (car global))
  678. (or (cdr local) (cdr global)))))))
  679. (defun org-cite-read-processor-declaration (s)
  680. "Read processor declaration from string S.
  681. Return (NAME BIBLIOGRAPHY-STYLE CITATION-STYLE) triplet, when
  682. NAME is the processor name, as a symbol, and both
  683. BIBLIOGRAPHY-STYLE and CITATION-STYLE are strings or nil. Those
  684. strings may contain spaces if they are enclosed within double
  685. quotes.
  686. String S is expected to contain between 1 and 3 tokens. The
  687. function raises an error when it contains too few or too many
  688. tokens. Spurious spaces are ignored."
  689. (with-temp-buffer
  690. (save-excursion (insert s))
  691. (let ((result (list (read (current-buffer)))))
  692. (dotimes (_ 2)
  693. (skip-chars-forward " \t")
  694. (cond
  695. ((eobp) (push nil result))
  696. ((char-equal ?\" (char-after))
  697. (push (org-not-nil (read (current-buffer)))
  698. result))
  699. (t
  700. (let ((origin (point)))
  701. (skip-chars-forward "^ \t")
  702. (push (org-not-nil (buffer-substring origin (point)))
  703. result)))))
  704. (skip-chars-forward " \t")
  705. (unless (eobp)
  706. (error "Trailing garbage following cite export processor declaration %S"
  707. s))
  708. (nreverse result))))
  709. (defun org-cite-bibliography-style (info)
  710. "Return expected bibliography style.
  711. INFO is a plist used as a communication channel."
  712. (pcase (plist-get info :cite-export)
  713. (`(,_ ,style ,_) style)
  714. (_ nil)))
  715. (defun org-cite-bibliography-properties (keyword)
  716. "Return properties associated to \"print_bibliography\" KEYWORD object.
  717. Return value is a property list."
  718. (org-cite--parse-as-plist (org-element-property :value keyword)))
  719. (defun org-cite-list-citations (info)
  720. "List citations in the exported document.
  721. Citations are ordered by appearance in the document, when following footnotes.
  722. INFO is the export communication channel, as a property list."
  723. (or (plist-get info :citations)
  724. (letrec ((cites nil)
  725. (tree (plist-get info :parse-tree))
  726. (definition-cache (make-hash-table :test #'equal))
  727. (definition-list nil)
  728. (find-definition
  729. ;; Find definition for standard reference LABEL. At
  730. ;; this point, it is impossible to rely on
  731. ;; `org-export-get-footnote-definition' because the
  732. ;; function caches results that could contain
  733. ;; un-processed citation objects. So we use
  734. ;; a simplified version of the function above.
  735. (lambda (label)
  736. (or (gethash label definition-cache)
  737. (org-element-map
  738. (or definition-list
  739. (setq definition-list
  740. (org-element-map
  741. tree
  742. 'footnote-definition
  743. #'identity info)))
  744. 'footnote-definition
  745. (lambda (d)
  746. (and (equal label (org-element-property :label d))
  747. (puthash label
  748. (or (org-element-contents d) "")
  749. definition-cache)))
  750. info t))))
  751. (search-cites
  752. (lambda (data)
  753. (org-element-map data '(citation footnote-reference)
  754. (lambda (datum)
  755. (pcase (org-element-type datum)
  756. ('citation (push datum cites))
  757. ;; Do not force entering inline definitions, since
  758. ;; `org-element-map' is going to enter it anyway.
  759. ((guard (eq 'inline (org-element-property :type datum))))
  760. ;; Walk footnote definition.
  761. (_
  762. (let ((label (org-element-property :label datum)))
  763. (funcall search-cites
  764. (funcall find-definition label)))))
  765. nil)
  766. info nil 'footnote-definition t))))
  767. (funcall search-cites tree)
  768. (let ((result (nreverse cites)))
  769. (plist-put info :citations result)
  770. result))))
  771. (defun org-cite-list-keys (info)
  772. "List citation keys in the exported document.
  773. Keys are ordered by first appearance in the document, when following footnotes.
  774. Duplicate keys are removed. INFO is the export communication channel, as a
  775. property list."
  776. (delete-dups
  777. (org-element-map (org-cite-list-citations info) 'citation-reference
  778. (lambda (r) (org-element-property :key r))
  779. info)))
  780. (defun org-cite-key-number (key info &optional predicate)
  781. "Return number associated to string KEY.
  782. INFO is the export communication channel, as a property list.
  783. Optional argument PREDICATE is called with two keys, and returns non-nil
  784. if the first reference should sort before the second. When nil, references
  785. are sorted in order cited."
  786. (let* ((keys (org-cite-list-keys info))
  787. (sorted-keys (if (functionp predicate)
  788. (sort keys predicate)
  789. keys))
  790. (position (seq-position sorted-keys key #'string-equal)))
  791. (and (integerp position)
  792. (1+ position))))
  793. (defun org-cite-inside-footnote-p (citation &optional strict)
  794. "Non-nil when CITATION object is contained within a footnote.
  795. When optional argument STRICT is non-nil, return t only if CITATION represents
  796. the sole contents of the footnote, e.g., after calling `org-cite-wrap-citation'.
  797. When non-nil, the return value if the footnote container."
  798. (let ((footnote
  799. (org-element-lineage citation
  800. '(footnote-definition footnote-reference))))
  801. (and footnote
  802. (or (not strict)
  803. (equal (org-element-contents (org-element-property :parent citation))
  804. (list citation)))
  805. ;; Return value.
  806. footnote)))
  807. (defun org-cite-wrap-citation (citation info)
  808. "Wrap an anonymous inline footnote around CITATION object in the parse tree.
  809. INFO is the export state, as a property list.
  810. White space before the citation, if any, are removed. The parse tree is
  811. modified by side-effect.
  812. Return newly created footnote object."
  813. (let ((footnote
  814. (list 'footnote-reference
  815. (list :label nil
  816. :type 'inline
  817. :contents-begin (org-element-property :begin citation)
  818. :contents-end (org-element-property :end citation)
  819. :post-blank (org-element-property :post-blank citation)))))
  820. ;; Remove any white space before citation.
  821. (org-cite--set-previous-post-blank citation 0 info)
  822. ;; Footnote swallows citation.
  823. (org-element-insert-before footnote citation)
  824. (org-element-adopt-elements footnote
  825. (org-element-extract-element citation))))
  826. (defun org-cite-adjust-note (citation info &optional rule punct)
  827. "Adjust note number location for CITATION object, and punctuation around it.
  828. INFO is the export state, as a property list.
  829. Optional argument RULE is the punctuation rule used, as a triplet. When nil,
  830. rule is determined according to `org-cite-note-rules', which see.
  831. Optional argument PUNCT is a list of punctuation marks to be considered.
  832. When nil, it defaults to `org-cite-punctuation-marks'.
  833. Parse tree is modified by side-effect.
  834. Note: when calling both `org-cite-adjust-note' and `org-cite-wrap-citation' on
  835. the same object, call `org-cite-adjust-note' first."
  836. (when org-cite-adjust-note-numbers
  837. (pcase-let* ((rule (or rule (org-cite--get-note-rule info)))
  838. (punct-re (regexp-opt (or punct org-cite-punctuation-marks)))
  839. ;; with Emacs <27.1. Argument of `regexp' form (PUNCT-RE this case)
  840. ;; must be a string literal.
  841. (previous-punct-re
  842. (rx-to-string `(seq (opt (group (regexp ,(rx (0+ (any blank ?\n))))
  843. (regexp ,punct-re)))
  844. (regexp ,(rx (opt (0+ (any blank ?\n)) (group ?\"))
  845. (opt (group (1+ (any blank ?\n))))
  846. string-end)))
  847. t))
  848. (next-punct-re
  849. (rx-to-string `(seq string-start
  850. (group (0+ (any blank ?\n)) (regexp ,punct-re)))
  851. t))
  852. (next (org-export-get-next-element citation info))
  853. (final-punct
  854. (and (stringp next)
  855. (string-match next-punct-re next)
  856. (match-string 1 next)))
  857. (previous
  858. ;; Find the closest terminal object. Consider
  859. ;; citation, subscript and superscript objects as
  860. ;; terminal.
  861. (org-last
  862. (org-element-map (org-export-get-previous-element citation info)
  863. '(citation code entity export-snippet footnote-reference
  864. line-break latex-fragment link plain-text
  865. radio-target statistics-cookie timestamp
  866. verbatim)
  867. #'identity info nil '(citation subscript superscript))))
  868. (`(,punct ,quote ,spacing)
  869. (and (stringp previous)
  870. (string-match previous-punct-re previous)
  871. (list (match-string 1 previous)
  872. (match-string 2 previous)
  873. (match-string 3 previous)))))
  874. ;; Bail you when there is no quote and either no punctuation, or
  875. ;; punctuation on both sides.
  876. (when (or quote (org-xor punct final-punct))
  877. ;; Phase 1: handle punctuation rule.
  878. (pcase rule
  879. ((guard (not quote)) nil)
  880. ;; Move punctuation inside.
  881. (`(,(or `inside (and `adaptive (guard (not spacing)))) . ,_)
  882. ;; This only makes sense if there is a quotation before the
  883. ;; citation that does not end with some punctuation.
  884. (when (and (not punct) final-punct)
  885. ;; Quote guarantees there is a string object before
  886. ;; citation. Likewise, any final punctuation guarantees
  887. ;; there is a string object following citation.
  888. (let ((new-prev
  889. (replace-regexp-in-string
  890. previous-punct-re
  891. (concat final-punct "\"") previous nil nil 2))
  892. (new-next
  893. (replace-regexp-in-string
  894. ;; Before Emacs-27.1 `literal' `rx' form with a variable
  895. ;; as an argument is not available.
  896. (rx-to-string `(seq string-start ,final-punct) t)
  897. "" next)))
  898. (org-element-set-element previous new-prev)
  899. (org-element-set-element next new-next)
  900. (setq previous new-prev)
  901. (setq next new-next)
  902. (setq punct final-punct)
  903. (setq final-punct nil))))
  904. ;; Move punctuation outside.
  905. (`(,(or `outside (and `adaptive (guard spacing))) . ,_)
  906. ;; This is only meaningful if there is some inner
  907. ;; punctuation and no final punctuation already.
  908. (when (and punct (not final-punct))
  909. ;; Inner punctuation guarantees there is text object
  910. ;; before the citation. However, there is no information
  911. ;; about the object following citation, if any.
  912. ;; Therefore, we handle all the possible cases (string,
  913. ;; other type, or none).
  914. (let ((new-prev
  915. (replace-regexp-in-string
  916. previous-punct-re "" previous nil nil 1))
  917. (new-next (if (stringp next) (concat punct next) punct)))
  918. (org-element-set-element previous new-prev)
  919. (cond
  920. ((stringp next)
  921. (org-element-set-element next new-next))
  922. (next
  923. (org-element-insert-before new-next next))
  924. (t
  925. (org-element-adopt-elements
  926. (org-element-property :parent citation)
  927. new-next)))
  928. (setq previous new-prev)
  929. (setq next new-next)
  930. (setq final-punct punct)
  931. (setq punct nil))))
  932. (_
  933. (error "Invalid punctuation rule: %S" rule))))
  934. ;; Phase 2: move citation to its appropriate location.
  935. ;;
  936. ;; First transform relative citation location into a definitive
  937. ;; location, according to the surrounding punctuation.
  938. (pcase rule
  939. (`(,punctuation same ,order)
  940. (setf rule
  941. (list punctuation
  942. (cond
  943. ;; When there is punctuation on both sides, the
  944. ;; citation is necessarily on the outside.
  945. ((and punct final-punct) 'outside)
  946. (punct 'inside)
  947. (final-punct 'outside)
  948. ;; No punctuation: bail out on next step.
  949. (t nil))
  950. order))))
  951. (pcase rule
  952. (`(,_ nil ,_) nil)
  953. (`(,_ inside after)
  954. ;; Citation has to be moved after punct, if there is
  955. ;; a quotation mark, or after final punctuation.
  956. (cond
  957. (quote
  958. (org-cite--insert-at-split previous citation 2 previous-punct-re))
  959. (final-punct
  960. (org-cite--move-punct-before final-punct citation next info))
  961. ;; There is only punct, and we're already after it.
  962. (t nil)))
  963. (`(,_ inside before)
  964. ;; Citation is already behind final-punct, so only consider
  965. ;; other locations.
  966. (when (or punct quote)
  967. (org-cite--insert-at-split previous citation 0 previous-punct-re)))
  968. (`(,_ outside after)
  969. ;; Citation is already after any punct or quote. It can only
  970. ;; move past final punctuation, if there is one.
  971. (when final-punct
  972. (org-cite--move-punct-before final-punct citation next info)))
  973. (`(,_ outside before)
  974. ;; The only non-trivial case is when citation follows punct
  975. ;; without a quote.
  976. (when (and punct (not quote))
  977. (org-cite--insert-at-split previous citation 0 previous-punct-re)))
  978. (_
  979. (error "Invalid punctuation rule: %S" rule))))))
  980. ;;; Tools generating or operating on parsed data
  981. (defun org-cite-parse-elements (s)
  982. "Parse string S as a list of Org elements.
  983. The return value is suitable as a replacement for a
  984. \"print_bibliography\" keyword. As a consequence, the function
  985. raises an error if S contains a headline."
  986. (with-temp-buffer
  987. (insert s)
  988. (pcase (org-element-contents (org-element-parse-buffer))
  989. ('nil nil)
  990. (`(,(and section (guard (eq 'section (org-element-type section)))))
  991. (org-element-contents section))
  992. (_
  993. (error "Headlines cannot replace a keyword")))))
  994. (defun org-cite-parse-objects (s &optional affix)
  995. "Parse string S as a secondary string.
  996. The return value is suitable as a replacement for a citation object.
  997. When optional argument AFFIX is non-nil, restrict the set of allowed object
  998. types to match the contents of a citation affix."
  999. (org-element-parse-secondary-string
  1000. s (org-element-restriction (if affix 'citation-reference 'paragraph))))
  1001. (defun org-cite-make-paragraph (&rest data)
  1002. "Return a paragraph element containing DATA.
  1003. DATA are strings, objects or secondary strings."
  1004. (apply #'org-element-create 'paragraph nil (apply #'org-cite-concat data)))
  1005. (defun org-cite-emphasize (type &rest data)
  1006. "Apply emphasis TYPE on DATA.
  1007. TYPE is a symbol among `bold', `italic', `strike-through' and `underline'.
  1008. DATA are strings, objects or secondary strings. Return an object of type TYPE."
  1009. (declare (indent 1))
  1010. (unless (memq type '(bold italic strike-through underline))
  1011. (error "Wrong emphasis type: %S" type))
  1012. (apply #'org-element-create type nil (apply #'org-cite-concat data)))
  1013. (defun org-cite-concat (&rest data)
  1014. "Concatenate all the DATA arguments and make the result a secondary string.
  1015. Each argument may be a string, an object, or a secondary string."
  1016. (let ((results nil))
  1017. (dolist (datum (reverse data))
  1018. (pcase datum
  1019. ('nil nil)
  1020. ;; Element or object.
  1021. ((pred org-element-type) (push datum results))
  1022. ;; Secondary string.
  1023. ((pred consp) (setq results (append datum results)))
  1024. (_
  1025. (signal
  1026. 'wrong-type-argument
  1027. (list (format "Argument is not a string or a secondary string: %S"
  1028. datum))))))
  1029. results))
  1030. (defun org-cite-mapconcat (function data separator)
  1031. "Apply FUNCTION to each element of DATA, and return a secondary string.
  1032. In between each pair of results, stick SEPARATOR, which may be a string,
  1033. an object, or a secondary string. FUNCTION must be a function of one argument,
  1034. and must return either a string, an object, or a secondary string."
  1035. (and data
  1036. (let ((result (list (funcall function (car data)))))
  1037. (dolist (datum (cdr data))
  1038. (setq result
  1039. (org-cite-concat result separator (funcall function datum))))
  1040. result)))
  1041. ;;; Internal interface with fontification (activate capability)
  1042. (defun org-cite-fontify-default (cite)
  1043. "Fontify CITE with `org-cite' and `org-cite-key' faces.
  1044. CITE is a citation object. The function applies `org-cite' face
  1045. on the whole citation, and `org-cite-key' face on each key."
  1046. (let ((beg (org-element-property :begin cite))
  1047. (end (org-with-point-at (org-element-property :end cite)
  1048. (skip-chars-backward " \t")
  1049. (point))))
  1050. (add-text-properties beg end '(font-lock-multiline t))
  1051. (add-face-text-property beg end 'org-cite)
  1052. (dolist (reference (org-cite-get-references cite))
  1053. (let ((boundaries (org-cite-key-boundaries reference)))
  1054. (add-face-text-property (car boundaries) (cdr boundaries)
  1055. 'org-cite-key)))))
  1056. (defun org-cite-activate (limit)
  1057. "Activate citations from up to LIMIT buffer position.
  1058. Each citation encountered is activated using the appropriate function
  1059. from the processor set in `org-cite-activate-processor'."
  1060. (let* ((name org-cite-activate-processor)
  1061. (activate
  1062. (or (and name
  1063. (org-cite-processor-has-capability-p name 'activate)
  1064. (org-cite-processor-activate (org-cite-get-processor name)))
  1065. #'org-cite-fontify-default)))
  1066. (when (re-search-forward org-element-citation-prefix-re limit t)
  1067. (let ((cite (org-with-point-at (match-beginning 0)
  1068. (org-element-citation-parser))))
  1069. (when cite
  1070. (funcall activate cite)
  1071. ;; Move after cite object and make sure to return
  1072. ;; a non-nil value.
  1073. (goto-char (org-element-property :end cite)))))))
  1074. ;;; Internal interface with Org Export library (export capability)
  1075. (defun org-cite-store-bibliography (info)
  1076. "Store bibliography in the communication channel.
  1077. Bibliography is stored as a list of absolute file names in the `:bibliography'
  1078. property.
  1079. INFO is the communication channel, as a plist. It is modified by side-effect."
  1080. (plist-put info :bibliography (org-cite-list-bibliography-files)))
  1081. (defun org-cite-store-export-processor (info)
  1082. "Store export processor in the `:cite-export' property during export.
  1083. Export processor is stored as a triplet, or nil.
  1084. When non-nil, it is defined as (NAME BIBLIOGRAPHY-STYLE
  1085. CITATION-STYLE) where NAME is a symbol, whereas
  1086. BIBLIOGRAPHY-STYLE and CITATION-STYLE are strings, or nil.
  1087. INFO is the communication channel, as a plist. It is modified by
  1088. side-effect."
  1089. (let* ((err
  1090. (lambda (s)
  1091. (user-error "Invalid cite export processor declaration: %S" s)))
  1092. (processor
  1093. (pcase (plist-get info :cite-export)
  1094. ((or "" `nil) nil)
  1095. ;; Value is a string. It comes from a "cite_export"
  1096. ;; keyword.
  1097. ((and (pred stringp) s)
  1098. (org-cite-read-processor-declaration s))
  1099. ;; Value is an alist. It must come from
  1100. ;; `org-cite-export-processors' variable. Find the most
  1101. ;; appropriate processor according to current export
  1102. ;; back-end.
  1103. ((and (pred consp) alist)
  1104. (let* ((backend (plist-get info :back-end))
  1105. (candidates
  1106. ;; Limit candidates to processors associated to
  1107. ;; back-ends derived from or equal to the current
  1108. ;; one.
  1109. (sort (seq-filter
  1110. (pcase-lambda (`(,key . ,_))
  1111. (org-export-derived-backend-p backend key))
  1112. alist)
  1113. (lambda (a b)
  1114. (org-export-derived-backend-p (car a) (car b))))))
  1115. ;; Select the closest candidate, or fallback to t.
  1116. (pcase (or (car candidates) (assq t alist))
  1117. ('nil nil)
  1118. (`(,_ . ,p)
  1119. ;; Normalize value by turning it into a triplet.
  1120. (pcase p
  1121. (`(,(pred symbolp))
  1122. (append p (list nil nil)))
  1123. (`(,(pred symbolp) ,(pred string-or-null-p))
  1124. (append p (list nil)))
  1125. (`(,(pred symbolp)
  1126. ,(pred string-or-null-p)
  1127. ,(pred string-or-null-p))
  1128. p)
  1129. (_ (funcall err p))))
  1130. (other (funcall err (cdr other))))))
  1131. (other (funcall err other)))))
  1132. (pcase processor
  1133. ('nil nil)
  1134. (`(,name . ,_)
  1135. (org-cite-try-load-processor name)
  1136. (cond
  1137. ((not (org-cite-get-processor name))
  1138. (user-error "Unknown processor %S" name))
  1139. ((not (org-cite-processor-has-capability-p name 'export))
  1140. (user-error "Processor %S is unable to handle citation export" name)))))
  1141. (plist-put info :cite-export processor)))
  1142. (defun org-cite-export-citation (citation _ info)
  1143. "Export CITATION object according to INFO property list.
  1144. This function delegates the export of the current citation to the
  1145. selected citation processor."
  1146. (pcase (plist-get info :cite-export)
  1147. ('nil nil)
  1148. (`(,p ,_ ,_)
  1149. (funcall (org-cite-processor-export-citation (org-cite-get-processor p))
  1150. citation
  1151. (org-cite-citation-style citation info)
  1152. (plist-get info :back-end)
  1153. info))
  1154. (other (error "Invalid `:cite-export' value: %S" other))))
  1155. (defun org-cite-export-bibliography (keyword _ info)
  1156. "Return bibliography associated to \"print_bibliography\" KEYWORD.
  1157. BACKEND is the export back-end, as a symbol. INFO is a plist
  1158. used as a communication channel."
  1159. (pcase (plist-get info :cite-export)
  1160. ('nil nil)
  1161. (`(,p ,_ ,_)
  1162. (let ((export-bibilography
  1163. (org-cite-processor-export-bibliography
  1164. (org-cite-get-processor p))))
  1165. (when export-bibilography
  1166. (funcall export-bibilography
  1167. (org-cite-list-keys info)
  1168. (plist-get info :bibliography)
  1169. (org-cite-bibliography-style info)
  1170. (org-cite-bibliography-properties keyword)
  1171. (plist-get info :back-end)
  1172. info))))
  1173. (other (error "Invalid `:cite-export' value: %S" other))))
  1174. (defun org-cite-process-citations (info)
  1175. "Replace all citations in the parse tree.
  1176. INFO is the communication channel, as a plist. Parse tree is modified
  1177. by side-effect."
  1178. (dolist (cite (org-cite-list-citations info))
  1179. (let ((replacement (org-cite-export-citation cite nil info))
  1180. (blanks (or (org-element-property :post-blank cite) 0)))
  1181. (if (null replacement)
  1182. ;; Before removing the citation, transfer its `:post-blank'
  1183. ;; property to the object before, if any.
  1184. (org-cite--set-previous-post-blank cite blanks info)
  1185. ;; Make sure there is a space between a quotation mark and
  1186. ;; a citation. This is particularly important when using
  1187. ;; `adaptive' note rule. See `org-cite-note-rules'.
  1188. (let ((previous (org-export-get-previous-element cite info)))
  1189. (when (and (org-string-nw-p previous)
  1190. (string-suffix-p "\"" previous))
  1191. (org-cite--set-previous-post-blank cite 1 info)))
  1192. (pcase replacement
  1193. ;; String.
  1194. ((pred stringp)
  1195. ;; Handle `:post-blank' before replacing value.
  1196. (let ((output (concat (org-trim replacement)
  1197. (make-string blanks ?\s))))
  1198. (org-element-insert-before (org-export-raw-string output) cite)))
  1199. ;; Single element.
  1200. (`(,(pred symbolp) . ,_)
  1201. (org-cite--set-post-blank replacement blanks)
  1202. (org-element-insert-before replacement cite))
  1203. ;; Secondary string: splice objects at cite's place.
  1204. ;; Transfer `:post-blank' to the last object.
  1205. ((pred consp)
  1206. (let ((last nil))
  1207. (dolist (datum replacement)
  1208. (setq last datum)
  1209. (org-element-insert-before datum cite))
  1210. (org-cite--set-post-blank last blanks)))
  1211. (_
  1212. (error "Invalid return value from citation export processor: %S"
  1213. replacement))))
  1214. (org-element-extract-element cite))))
  1215. (defun org-cite-process-bibliography (info)
  1216. "Replace all \"print_bibliography\" keywords in the parse tree.
  1217. INFO is the communication channel, as a plist. Parse tree is modified
  1218. by side effect."
  1219. (org-element-map (plist-get info :parse-tree) 'keyword
  1220. (lambda (keyword)
  1221. (when (equal "PRINT_BIBLIOGRAPHY" (org-element-property :key keyword))
  1222. (let ((replacement (org-cite-export-bibliography keyword nil info))
  1223. (blanks (or (org-element-property :post-blank keyword) 0)))
  1224. (pcase replacement
  1225. ;; Before removing the citation, transfer its
  1226. ;; `:post-blank' property to the element before, if any.
  1227. ('nil
  1228. (org-cite--set-previous-post-blank keyword blanks info)
  1229. (org-element-extract-element keyword))
  1230. ;; Handle `:post-blank' before replacing keyword with string.
  1231. ((pred stringp)
  1232. (let ((output (concat (org-element-normalize-string replacement)
  1233. (make-string blanks ?\n))))
  1234. (org-element-set-element keyword (org-export-raw-string output))))
  1235. ;; List of elements: splice contents before keyword and
  1236. ;; remove the latter. Transfer `:post-blank' to last
  1237. ;; element.
  1238. ((and `(,(pred listp) . ,_) contents)
  1239. (let ((last nil))
  1240. (dolist (datum contents)
  1241. (setq last datum)
  1242. (org-element-insert-before datum keyword))
  1243. (org-cite--set-post-blank last blanks)
  1244. (org-element-extract-element keyword)))
  1245. ;; Single element: replace the keyword.
  1246. (`(,(pred symbolp) . ,_)
  1247. (org-cite--set-post-blank replacement blanks)
  1248. (org-element-set-element keyword replacement))
  1249. (_
  1250. (error "Invalid return value from citation export processor: %S"
  1251. replacement))))))
  1252. info))
  1253. (defun org-cite-finalize-export (output info)
  1254. "Finalizer for export process.
  1255. OUTPUT is the full output of the export process. INFO is the communication
  1256. channel, as a property list."
  1257. (pcase (plist-get info :cite-export)
  1258. ('nil output)
  1259. (`(,p ,_ ,_)
  1260. (let ((finalizer
  1261. (org-cite-processor-export-finalizer (org-cite-get-processor p))))
  1262. (if (not finalizer)
  1263. output
  1264. (funcall finalizer
  1265. output
  1266. (org-cite-list-keys info)
  1267. (plist-get info :bibliography)
  1268. (org-cite-bibliography-style info)
  1269. (plist-get info :back-end)
  1270. info))))
  1271. (other (error "Invalid `:cite-export' value: %S" other))))
  1272. ;;; Internal interface with `org-open-at-point' (follow capability)
  1273. (defun org-cite-follow (datum arg)
  1274. "Follow citation or citation-reference DATUM.
  1275. Following is done according to the processor set in `org-cite-follow-processor'.
  1276. ARG is the prefix argument received when calling `org-open-at-point', or nil."
  1277. (unless org-cite-follow-processor
  1278. (user-error "No processor set to follow citations"))
  1279. (org-cite-try-load-processor org-cite-follow-processor)
  1280. (let ((name org-cite-follow-processor))
  1281. (cond
  1282. ((not (org-cite-get-processor name))
  1283. (user-error "Unknown processor %S" name))
  1284. ((not (org-cite-processor-has-capability-p name 'follow))
  1285. (user-error "Processor %S cannot follow citations" name))
  1286. (t
  1287. (let ((follow (org-cite-processor-follow (org-cite-get-processor name))))
  1288. (funcall follow datum arg))))))
  1289. ;;; Meta-command for citation insertion (insert capability)
  1290. (defun org-cite--allowed-p (context)
  1291. "Non-nil when a citation can be inserted at point.
  1292. CONTEXT is the element or object at point, as returned by `org-element-context'."
  1293. (let ((type (org-element-type context)))
  1294. (cond
  1295. ;; No citation in attributes, except in parsed ones.
  1296. ;;
  1297. ;; XXX: Inserting citation in a secondary value is not allowed
  1298. ;; yet. Is it useful?
  1299. ((let ((post (org-element-property :post-affiliated context)))
  1300. (and post (< (point) post)))
  1301. (let ((case-fold-search t))
  1302. (looking-back
  1303. (rx-to-string
  1304. `(seq line-start (0+ (any " \t"))
  1305. "#+"
  1306. (or ,@org-element-parsed-keywords)
  1307. ":"
  1308. (0+ nonl))
  1309. t)
  1310. (line-beginning-position))))
  1311. ;; Paragraphs and blank lines at top of document are fine.
  1312. ((memq type '(nil paragraph)))
  1313. ;; So are contents of verse blocks.
  1314. ((eq type 'verse-block)
  1315. (and (>= (point) (org-element-property :contents-begin context))
  1316. (< (point) (org-element-property :contents-end context))))
  1317. ;; In an headline or inlinetask, point must be either on the
  1318. ;; heading itself or on the blank lines below.
  1319. ((memq type '(headline inlinetask))
  1320. (or (not (org-at-heading-p))
  1321. (and (save-excursion
  1322. (beginning-of-line)
  1323. (and (let ((case-fold-search t))
  1324. (not (looking-at-p "\\*+ END[ \t]*$")))
  1325. (let ((case-fold-search nil))
  1326. (looking-at org-complex-heading-regexp))))
  1327. (match-beginning 4)
  1328. (>= (point) (match-beginning 4))
  1329. (or (not (match-beginning 5))
  1330. (< (point) (match-beginning 5))))))
  1331. ;; White spaces after an object or blank lines after an element
  1332. ;; are OK.
  1333. ((>= (point)
  1334. (save-excursion (goto-char (org-element-property :end context))
  1335. (skip-chars-backward " \r\t\n")
  1336. (if (eq (org-element-class context) 'object) (point)
  1337. (line-beginning-position 2)))))
  1338. ;; At the beginning of a footnote definition, right after the
  1339. ;; label, is OK.
  1340. ((eq type 'footnote-definition) (looking-at (rx space)))
  1341. ;; At the start of a list item is fine, as long as the bullet is
  1342. ;; unaffected.
  1343. ((eq type 'item)
  1344. (> (point) (+ (org-element-property :begin context)
  1345. (current-indentation)
  1346. (if (org-element-property :checkbox context)
  1347. 5 1))))
  1348. ;; Other elements are invalid.
  1349. ((eq (org-element-class context) 'element) nil)
  1350. ;; Just before object is fine.
  1351. ((= (point) (org-element-property :begin context)))
  1352. ;; Within recursive object too, but not in a link.
  1353. ((eq type 'link) nil)
  1354. ((eq type 'table-cell)
  1355. ;; :contents-begin is not reliable on empty cells, so special
  1356. ;; case it.
  1357. (<= (save-excursion (skip-chars-backward " \t") (point))
  1358. (org-element-property :contents-end context)))
  1359. ((let ((cbeg (org-element-property :contents-begin context))
  1360. (cend (org-element-property :contents-end context)))
  1361. (and cbeg (>= (point) cbeg) (<= (point) cend)))))))
  1362. (defun org-cite--insert-string-before (string reference)
  1363. "Insert STRING before citation REFERENCE object."
  1364. (org-with-point-at (org-element-property :begin reference)
  1365. (insert string ";")))
  1366. (defun org-cite--insert-string-after (string reference)
  1367. "Insert STRING after citation REFERENCE object."
  1368. (org-with-point-at (org-element-property :end reference)
  1369. ;; Make sure to move forward when we're inserting at point, so the
  1370. ;; insertion can happen multiple times.
  1371. (if (char-equal ?\; (char-before))
  1372. (insert-before-markers string ";")
  1373. (insert-before-markers ";" string))))
  1374. (defun org-cite--keys-to-citation (keys)
  1375. "Build a citation object from a list of citation KEYS.
  1376. Citation keys are strings without the leading \"@\"."
  1377. (apply #'org-element-create
  1378. 'citation
  1379. nil
  1380. (mapcar (lambda (k)
  1381. (org-element-create 'citation-reference (list :key k)))
  1382. keys)))
  1383. (defun org-cite-make-insert-processor (select-key select-style)
  1384. "Build a function appropriate as an insert processor.
  1385. SELECT-KEY is a function called with one argument. When it is
  1386. nil, the function should return a citation key as a string, or
  1387. nil. Otherwise, the function should return a list of such keys,
  1388. or nil. The keys should not have any leading \"@\" character.
  1389. SELECT-STYLE is a function called with one argument, the citation
  1390. object being edited or constructed so far. It should return
  1391. a style string, or nil.
  1392. The return value is a function of two arguments: CONTEXT and ARG.
  1393. CONTEXT is either a citation reference, a citation object, or
  1394. nil. ARG is a prefix argument.
  1395. The generated function inserts or edits a citation at point.
  1396. More specifically,
  1397. On a citation reference:
  1398. - on the prefix or right before the \"@\" character, insert
  1399. a new reference before the current one,
  1400. - on the suffix, insert it after the reference,
  1401. - otherwise, update the cite key, preserving both affixes.
  1402. When ARG is non-nil, remove the reference, possibly removing
  1403. the whole citation if it contains a single reference.
  1404. On a citation object:
  1405. - on the style part, offer to update it,
  1406. - on the global prefix, add a new reference before the first
  1407. one,
  1408. - on the global suffix, add a new reference after the last
  1409. one.
  1410. Elsewhere, insert a citation at point. When ARG is non-nil,
  1411. offer to complete style in addition to references."
  1412. (unless (and (functionp select-key) (functionp select-style))
  1413. (error "Wrong argument type(s)"))
  1414. (lambda (context arg)
  1415. (pcase (org-element-type context)
  1416. ;; When on a citation, check point is not on the blanks after it.
  1417. ;; Otherwise, consider we're after it.
  1418. ((and 'citation
  1419. (guard
  1420. (let ((boundaries (org-cite-boundaries context)))
  1421. (and (< (point) (cdr boundaries))
  1422. (> (point) (car boundaries))))))
  1423. ;; When ARG is non-nil, delete the whole citation. Otherwise,
  1424. ;; action depends on the point.
  1425. (if arg
  1426. (org-cite-delete-citation context)
  1427. (let* ((begin (org-element-property :begin context))
  1428. (style-end (1- (org-with-point-at begin (search-forward ":")))))
  1429. (if (>= style-end (point))
  1430. ;; On style part, edit the style.
  1431. (let ((style-start (+ 5 begin))
  1432. (style (funcall select-style)))
  1433. (unless style (user-error "Aborted"))
  1434. (org-with-point-at style-start
  1435. (delete-region style-start style-end)
  1436. (when (org-string-nw-p style) (insert "/" style))))
  1437. ;; On an affix, insert a new reference before or after
  1438. ;; point.
  1439. (let* ((references (org-cite-get-references context))
  1440. (key (concat "@" (funcall select-key nil))))
  1441. (if (< (point) (org-element-property :contents-begin context))
  1442. (org-cite--insert-string-before key (car references))
  1443. (org-cite--insert-string-after key (org-last references))))))))
  1444. ;; On a citation reference. If ARG is not nil, remove the
  1445. ;; reference. Otherwise, action depends on the point.
  1446. ((and 'citation-reference (guard arg)) (org-cite-delete-citation context))
  1447. ('citation-reference
  1448. (pcase-let* ((`(,start . ,end) (org-cite-key-boundaries context))
  1449. (key (concat "@"
  1450. (or (funcall select-key nil)
  1451. (user-error "Aborted")))))
  1452. ;; Right before the "@" character, do not replace the reference
  1453. ;; at point, but insert a new one before it. It makes adding
  1454. ;; a new reference at the beginning easier in the following
  1455. ;; case: [cite:@key].
  1456. (cond
  1457. ((>= start (point)) (org-cite--insert-string-before key context))
  1458. ((<= end (point)) (org-cite--insert-string-after key context))
  1459. (t
  1460. (org-with-point-at start
  1461. (delete-region start end)
  1462. (insert key))))))
  1463. (_
  1464. (let ((keys (funcall select-key t)))
  1465. (unless keys (user-error "Aborted"))
  1466. (insert
  1467. (format "[cite%s:%s]"
  1468. (if arg
  1469. (let ((style (funcall select-style
  1470. (org-cite--keys-to-citation keys))))
  1471. (if (org-string-nw-p style)
  1472. (concat "/" style)
  1473. ""))
  1474. "")
  1475. (mapconcat (lambda (k) (concat "@" k)) keys "; "))))))))
  1476. ;;;###autoload
  1477. (defun org-cite-insert (arg)
  1478. "Insert a citation at point.
  1479. Insertion is done according to the processor set in `org-cite-insert-processor'.
  1480. ARG is the prefix argument received when calling interactively the function."
  1481. (interactive "P")
  1482. (unless org-cite-insert-processor
  1483. (user-error "No processor set to insert citations"))
  1484. (org-cite-try-load-processor org-cite-insert-processor)
  1485. (let ((name org-cite-insert-processor))
  1486. (cond
  1487. ((not (org-cite-get-processor name))
  1488. (user-error "Unknown processor %S" name))
  1489. ((not (org-cite-processor-has-capability-p name 'insert))
  1490. (user-error "Processor %S cannot insert citations" name))
  1491. (t
  1492. (let ((context (org-element-context))
  1493. (insert (org-cite-processor-insert (org-cite-get-processor name))))
  1494. (cond
  1495. ((memq (org-element-type context) '(citation citation-reference))
  1496. (funcall insert context arg))
  1497. ((org-cite--allowed-p context)
  1498. (funcall insert nil arg))
  1499. (t
  1500. (user-error "Cannot insert a citation here"))))))))
  1501. (provide 'oc)
  1502. ;;; oc.el ends here