oc.el 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. ;;; oc.el --- Org Cite library -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 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. Return a non-nil value on a successful operation."
  323. (declare (indent 1))
  324. (unless (and name (symbolp name))
  325. (error "Invalid processor name: %S" name))
  326. (setq org-cite--processors
  327. (cons (apply #'org-cite--make-processor :name name body)
  328. (seq-remove (lambda (p) (eq name (org-cite-processor-name p)))
  329. org-cite--processors))))
  330. (defun org-cite-get-processor (name)
  331. "Return citation processor named after symbol NAME.
  332. Return nil if no such processor is found."
  333. ;; Opportunistically try to load the library providing the
  334. ;; processor.
  335. (require (intern (concat "oc-" (symbol-name name))) nil t)
  336. (seq-find (lambda (p) (eq name (org-cite-processor-name p)))
  337. org-cite--processors))
  338. (defun org-cite-unregister-processor (name)
  339. "Unregister citation processor NAME.
  340. NAME is a symbol. Raise an error if processor is not registered.
  341. Return a non-nil value on a successful operation."
  342. (unless (and name (symbolp name))
  343. (error "Invalid processor name: %S" name))
  344. (pcase (org-cite-get-processor name)
  345. ('nil (error "Processor %S not registered" name))
  346. (processor
  347. (setq org-cite--processors (delete processor org-cite--processors))))
  348. t)
  349. (defun org-cite-processor-has-capability-p (processor capability)
  350. "Return non-nil if PROCESSOR is able to handle CAPABILITY.
  351. PROCESSOR is the name of a cite processor, as a symbol. CAPABILITY is
  352. `activate', `export', `follow', or `insert'."
  353. (let ((p (org-cite-get-processor processor)))
  354. (pcase capability
  355. ((guard (not p)) nil) ;undefined processor
  356. ('activate (functionp (org-cite-processor-activate p)))
  357. ('export (functionp (org-cite-processor-export-citation p)))
  358. ('follow (functionp (org-cite-processor-follow p)))
  359. ('insert (functionp (org-cite-processor-insert p)))
  360. (other (error "Invalid capability: %S" other)))))
  361. ;;; Internal functions
  362. (defun org-cite--set-post-blank (datum blanks)
  363. "Set `:post-blank' property from element or object before DATUM to BLANKS.
  364. DATUM is an element or object. BLANKS is an integer. DATUM is modified
  365. by side-effect."
  366. (if (not (eq 'plain-text (org-element-type datum)))
  367. (org-element-put-property datum :post-blank blanks)
  368. ;; Remove any blank from string before DATUM so it is exported
  369. ;; with exactly BLANKS white spaces.
  370. (org-element-set-element
  371. datum
  372. (replace-regexp-in-string
  373. "[ \t\n]*\\'" (make-string blanks ?\s) datum))))
  374. (defun org-cite--set-previous-post-blank (datum blanks info)
  375. "Set `:post-blank' property from element or object before DATUM to BLANKS.
  376. DATUM is an element or object. BLANKS is an integer. INFO is the export
  377. state, as a property list. Previous element or object, if any, is modified by
  378. side-effect."
  379. (let ((previous (org-export-get-previous-element datum info)))
  380. (when previous
  381. (org-cite--set-post-blank previous blanks))))
  382. (defun org-cite--insert-at-split (s citation n regexp)
  383. "Split string S and insert CITATION object between the two parts.
  384. S is split at beginning of match group N upon matching REGEXP against it.
  385. This function assumes S precedes CITATION."
  386. ;; When extracting the citation, remove white spaces before it, but
  387. ;; preserve those after it.
  388. (let ((post-blank (org-element-property :post-blank citation)))
  389. (when (and post-blank (> post-blank 0))
  390. (org-element-insert-before (make-string post-blank ?\s) citation)))
  391. (org-element-insert-before
  392. (org-element-put-property (org-element-extract-element citation)
  393. :post-blank 0)
  394. s)
  395. (string-match regexp s)
  396. (let* ((split (match-beginning n))
  397. (first-part (substring s nil split))
  398. ;; Remove trailing white spaces as they are before the
  399. ;; citation.
  400. (last-part
  401. (replace-regexp-in-string (rx (1+ (any blank ?\n)) string-end)
  402. ""
  403. (substring s split))))
  404. (when (org-string-nw-p first-part)
  405. (org-element-insert-before first-part citation))
  406. (org-element-set-element s last-part)))
  407. (defun org-cite--move-punct-before (punct citation s info)
  408. "Move punctuation PUNCT before CITATION object.
  409. String S contains PUNCT. INFO is the export state, as a property list.
  410. The function assumes S follows CITATION. Parse tree is modified by side-effect."
  411. (if (equal s punct)
  412. (org-element-extract-element s) ;it would be empty anyway
  413. (org-element-set-element s (substring s (length punct))))
  414. ;; Remove blanks before citation.
  415. (org-cite--set-previous-post-blank citation 0 info)
  416. (org-element-insert-before
  417. ;; Blanks between citation and punct are now before punct and
  418. ;; citation.
  419. (concat (make-string (or (org-element-property :post-blank citation) 0) ?\s)
  420. punct)
  421. citation))
  422. (defun org-cite--parse-as-plist (s)
  423. "Parse string S as a property list.
  424. Values are always strings. Return nil if S is nil."
  425. (cond
  426. ((null s) nil)
  427. ((stringp s)
  428. (with-temp-buffer
  429. (save-excursion (insert s))
  430. (skip-chars-forward " \t")
  431. (let ((results nil)
  432. (value-flag nil))
  433. (while (not (eobp))
  434. (pcase (char-after)
  435. (?:
  436. (push (read (current-buffer)) results)
  437. (setq value-flag t))
  438. ((guard (not value-flag))
  439. (skip-chars-forward "^ \t"))
  440. (?\"
  441. (let ((origin (point)))
  442. (condition-case _
  443. (progn
  444. (read (current-buffer))
  445. (push (buffer-substring (1+ origin) (1- (point))) results))
  446. (end-of-file
  447. (goto-char origin)
  448. (skip-chars-forward "^ \t")
  449. (push (buffer-substring origin (point)) results)))
  450. (setq value-flag nil)))
  451. (_
  452. (let ((origin (point)))
  453. (skip-chars-forward "^ \t")
  454. (push (buffer-substring origin (point)) results)
  455. (setq value-flag nil))))
  456. (skip-chars-forward " \t"))
  457. (nreverse results))))
  458. (t (error "Invalid argument type: %S" s))))
  459. (defun org-cite--get-note-rule (info)
  460. "Return punctuation rule according to language used for export.
  461. INFO is the export state, as a property list.
  462. Rule is found according to the language used for export and
  463. `org-cite-note-rules', which see.
  464. If there is no rule matching current language, the rule defaults
  465. to (adaptive outside after)."
  466. (let* ((language-tags
  467. ;; Normalize language as a language-region tag, as described
  468. ;; in RFC 4646.
  469. (pcase (split-string (plist-get info :language) "[-_]")
  470. (`(,language)
  471. (list language
  472. (or (cdr (assoc language org-cite--default-region-alist))
  473. language)))
  474. (`(,language ,region)
  475. (list language region))
  476. (other
  477. (error "Invalid language identifier: %S" other))))
  478. (language-region (mapconcat #'downcase language-tags "-"))
  479. (language (car language-tags)))
  480. (or (cdr (assoc language-region org-cite-note-rules))
  481. (cdr (assoc language org-cite-note-rules))
  482. '(adaptive outside after))))
  483. ;;; Generic tools
  484. (defun org-cite-list-bibliography-files ()
  485. "List all bibliography files defined in the buffer."
  486. (delete-dups
  487. (append (mapcar (lambda (value)
  488. (pcase value
  489. (`(,f . ,d)
  490. (expand-file-name (org-strip-quotes f) d))))
  491. (pcase (org-collect-keywords
  492. '("BIBLIOGRAPHY") nil '("BIBLIOGRAPHY"))
  493. (`(("BIBLIOGRAPHY" . ,pairs)) pairs)))
  494. org-cite-global-bibliography)))
  495. (defun org-cite-get-references (citation &optional keys-only)
  496. "Return citations references contained in CITATION object.
  497. When optional argument KEYS-ONLY is non-nil, return the references' keys, as a
  498. list of strings.
  499. Assume CITATION object comes from either a full parse tree, e.g., during export,
  500. or from the current buffer."
  501. (let ((contents (org-element-contents citation)))
  502. (cond
  503. ((null contents)
  504. (org-with-point-at (org-element-property :contents-begin citation)
  505. (narrow-to-region (point) (org-element-property :contents-end citation))
  506. (let ((references nil))
  507. (while (not (eobp))
  508. (let ((reference (org-element-citation-reference-parser)))
  509. (goto-char (org-element-property :end reference))
  510. (push (if keys-only
  511. (org-element-property :key reference)
  512. reference)
  513. references)))
  514. (nreverse references))))
  515. (keys-only (mapcar (lambda (r) (org-element-property :key r)) contents))
  516. (t contents))))
  517. (defun org-cite-boundaries (citation)
  518. "Return the beginning and end strict position of CITATION.
  519. Returns a (BEG . END) pair."
  520. (let ((beg (org-element-property :begin citation))
  521. (end (org-with-point-at (org-element-property :end citation)
  522. (skip-chars-backward " \t")
  523. (point))))
  524. (cons beg end)))
  525. (defun org-cite-key-boundaries (reference)
  526. "Return citation REFERENCE's key boundaries as buffer positions.
  527. The function returns a pair (START . END) where START and END denote positions
  528. in the current buffer. Positions include leading \"@\" character."
  529. (org-with-point-at (org-element-property :begin reference)
  530. (let ((end (org-element-property :end reference)))
  531. (re-search-forward org-element-citation-key-re end t)
  532. (cons (match-beginning 0) (match-end 0)))))
  533. (defun org-cite-main-affixes (citation)
  534. "Return main affixes for CITATION object.
  535. Some export back-ends only support a single pair of affixes per
  536. citation, even if it contains multiple keys. This function
  537. decides what affixes are the most appropriate.
  538. Return a pair (PREFIX . SUFFIX) where PREFIX and SUFFIX are
  539. parsed data."
  540. (let ((source
  541. ;; When there are multiple references, use global affixes.
  542. ;; Otherwise, local affixes have priority.
  543. (pcase (org-cite-get-references citation)
  544. (`(,reference) reference)
  545. (_ citation))))
  546. (cons (org-element-property :prefix source)
  547. (org-element-property :suffix source))))
  548. (defun org-cite-supported-styles (&optional processors)
  549. "List of supported citation styles and variants.
  550. Supported styles are those handled by export processors from
  551. `org-cite-export-processors', or in PROCESSORS, as a list of symbols,
  552. when non-nil.
  553. Return value is a list with the following items:
  554. ((STYLE . SHORTCUTS) . VARIANTS))
  555. where STYLE is a string, SHORTCUTS a list of strings, and VARIANTS is a list of
  556. pairs (VARIANT . SHORTCUTS), VARIANT being a string and SHORTCUTS a list of
  557. strings."
  558. (let ((collection
  559. (seq-mapcat
  560. (lambda (name)
  561. (org-cite-processor-cite-styles (org-cite-get-processor name)))
  562. (or processors
  563. (mapcar (pcase-lambda (`(,_ . (,name . ,_))) name)
  564. org-cite-export-processors))))
  565. (result nil))
  566. ;; Merge duplicate styles. Each style full name is guaranteed to
  567. ;; be unique, and associated to all shortcuts and all variants in
  568. ;; the initial collection.
  569. (pcase-dolist (`((,style . ,shortcuts) . ,variants) collection)
  570. (let ((entry (assoc style result)))
  571. (if (not entry)
  572. (push (list style shortcuts variants) result)
  573. (setf (nth 1 entry)
  574. (seq-uniq (append shortcuts (nth 1 entry))))
  575. (setf (nth 2 entry)
  576. (append variants (nth 2 entry))))))
  577. ;; Return value with the desired format.
  578. (nreverse
  579. (mapcar (pcase-lambda (`(,style ,shortcuts ,variants))
  580. (cons (cons style (nreverse shortcuts))
  581. ;; Merge variant shortcuts.
  582. (let ((result nil))
  583. (pcase-dolist (`(,variant . ,shortcuts) variants)
  584. (let ((entry (assoc variant result)))
  585. (if (not entry)
  586. (push (cons variant shortcuts) result)
  587. (setf (cdr entry)
  588. (seq-uniq (append shortcuts (cdr entry)))))))
  589. result)))
  590. result))))
  591. (defun org-cite-delete-citation (datum)
  592. "Delete citation or citation reference DATUM.
  593. When removing the last reference, also remove the whole citation."
  594. (pcase (org-element-type datum)
  595. ('citation
  596. (pcase-let* ((`(,begin . ,end) (org-cite-boundaries datum))
  597. (pos-before-blank
  598. (org-with-point-at begin
  599. (skip-chars-backward " \t")
  600. (point)))
  601. (pos-after-blank (org-element-property :end datum))
  602. (first-on-line?
  603. (= pos-before-blank (line-beginning-position)))
  604. (last-on-line?
  605. (= pos-after-blank (line-end-position))))
  606. (cond
  607. ;; The citation is alone on its line. Remove the whole line.
  608. ;; Do not leave it blank as it might break a surrounding
  609. ;; paragraph.
  610. ((and first-on-line? last-on-line?)
  611. (delete-region (line-beginning-position) (line-beginning-position 2)))
  612. ;; When the citation starts the line, preserve indentation.
  613. (first-on-line? (delete-region begin pos-after-blank))
  614. ;; When the citation ends the line, remove any trailing space.
  615. (last-on-line? (delete-region pos-before-blank (line-end-position)))
  616. ;; Otherwise, delete blanks before the citation.
  617. ;; Nevertheless, make sure there is at least one blank left,
  618. ;; so as to not splice unrelated surroundings.
  619. (t
  620. (delete-region pos-before-blank end)
  621. (when (= pos-after-blank end)
  622. (org-with-point-at pos-before-blank (insert " ")))))))
  623. ('citation-reference
  624. (let* ((citation (org-element-property :parent datum))
  625. (references (org-cite-get-references citation))
  626. (begin (org-element-property :begin datum))
  627. (end (org-element-property :end datum)))
  628. (cond
  629. ;; Single reference.
  630. ((= 1 (length references))
  631. (org-cite-delete-citation citation))
  632. ;; First reference, no prefix.
  633. ((and (= begin (org-element-property :contents-begin citation))
  634. (not (org-element-property :prefix citation)))
  635. (org-with-point-at (org-element-property :begin datum)
  636. (skip-chars-backward " \t")
  637. (delete-region (point) end)))
  638. ;; Last reference, no suffix.
  639. ((and (= end (org-element-property :contents-end citation))
  640. (not (org-element-property :suffix citation)))
  641. (delete-region (1- begin) (1- (cdr (org-cite-boundaries citation)))))
  642. ;; Somewhere in-between.
  643. (t
  644. (delete-region begin end)))))
  645. (other
  646. (error "Invalid object type: %S" other))))
  647. ;;; Tools only available during export
  648. (defun org-cite-citation-style (citation info)
  649. "Return citation style used for CITATION object.
  650. Style is a pair (NAME . VARIANT) where NAME and VARIANT are strings or nil.
  651. A nil NAME means the default style for the current processor should be used.
  652. INFO is a plist used as a communication channel."
  653. (let* ((separate
  654. (lambda (s)
  655. (cond
  656. ((null s) (cons nil nil))
  657. ((not (string-match "/" s)) (cons s nil))
  658. (t (cons (substring s nil (match-beginning 0))
  659. (org-string-nw-p (substring s (match-end 0))))))))
  660. (local (funcall separate (org-element-property :style citation)))
  661. (global
  662. (funcall separate (pcase (plist-get info :cite-export)
  663. (`(,_ ,_ ,style) style)
  664. (_ nil)))))
  665. (cond
  666. ((org-string-nw-p (car local))
  667. (cons (org-not-nil (car local)) (cdr local)))
  668. (t
  669. (cons (org-not-nil (car global))
  670. (or (cdr local) (cdr global)))))))
  671. (defun org-cite-read-processor-declaration (s)
  672. "Read processor declaration from string S.
  673. Return (NAME BIBLIOGRAPHY-STYLE CITATION-STYLE) triplet, when
  674. NAME is the processor name, as a symbol, and both
  675. BIBLIOGRAPHY-STYLE and CITATION-STYLE are strings or nil. Those
  676. strings may contain spaces if they are enclosed within double
  677. quotes.
  678. String S is expected to contain between 1 and 3 tokens. The
  679. function raises an error when it contains too few or too many
  680. tokens. Spurious spaces are ignored."
  681. (with-temp-buffer
  682. (save-excursion (insert s))
  683. (let ((result (list (read (current-buffer)))))
  684. (dotimes (_ 2)
  685. (skip-chars-forward " \t")
  686. (cond
  687. ((eobp) (push nil result))
  688. ((char-equal ?\" (char-after))
  689. (push (org-not-nil (read (current-buffer)))
  690. result))
  691. (t
  692. (let ((origin (point)))
  693. (skip-chars-forward "^ \t")
  694. (push (org-not-nil (buffer-substring origin (point)))
  695. result)))))
  696. (skip-chars-forward " \t")
  697. (unless (eobp)
  698. (error "Trailing garbage following cite export processor declaration %S"
  699. s))
  700. (nreverse result))))
  701. (defun org-cite-bibliography-style (info)
  702. "Return expected bibliography style.
  703. INFO is a plist used as a communication channel."
  704. (pcase (plist-get info :cite-export)
  705. (`(,_ ,style ,_) style)
  706. (_ nil)))
  707. (defun org-cite-bibliography-properties (keyword)
  708. "Return properties associated to \"print_bibliography\" KEYWORD object.
  709. Return value is a property list."
  710. (org-cite--parse-as-plist (org-element-property :value keyword)))
  711. (defun org-cite-list-citations (info)
  712. "List citations in the exported document.
  713. Citations are ordered by appearance in the document, when following footnotes.
  714. INFO is the export communication channel, as a property list."
  715. (or (plist-get info :citations)
  716. (letrec ((cites nil)
  717. (tree (plist-get info :parse-tree))
  718. (find-definition
  719. ;; Find definition for standard reference LABEL. At
  720. ;; this point, it is impossible to rely on
  721. ;; `org-export-get-footnote-definition' because the
  722. ;; function caches results that could contain
  723. ;; un-processed citation objects. So we use
  724. ;; a simplified version of the function above.
  725. (lambda (label)
  726. (org-element-map tree 'footnote-definition
  727. (lambda (d)
  728. (and (equal label (org-element-property :label d))
  729. (or (org-element-contents d) "")))
  730. info t)))
  731. (search-cites
  732. (lambda (data)
  733. (org-element-map data '(citation footnote-reference)
  734. (lambda (datum)
  735. (pcase (org-element-type datum)
  736. ('citation (push datum cites))
  737. ;; Do not force entering inline definitions, since
  738. ;; `org-element-map' is going to enter it anyway.
  739. ((guard (eq 'inline (org-element-property :type datum))))
  740. ;; Walk footnote definition.
  741. (_
  742. (let ((label (org-element-property :label datum)))
  743. (funcall search-cites
  744. (funcall find-definition label))))))
  745. info nil 'footnote-definition t))))
  746. (funcall search-cites tree)
  747. (let ((result (nreverse cites)))
  748. (plist-put info :citations result)
  749. result))))
  750. (defun org-cite-list-keys (info)
  751. "List citation keys in the exported document.
  752. Keys are ordered by first appearance in the document, when following footnotes.
  753. Duplicate keys are removed. INFO is the export communication channel, as a
  754. property list."
  755. (delete-dups
  756. (org-element-map (org-cite-list-citations info) 'citation-reference
  757. (lambda (r) (org-element-property :key r))
  758. info)))
  759. (defun org-cite-key-number (key info &optional predicate)
  760. "Return number associated to string KEY.
  761. INFO is the export communication channel, as a property list.
  762. Optional argument PREDICATE is called with two keys, and returns non-nil
  763. if the first reference should sort before the second. When nil, references
  764. are sorted in order cited."
  765. (let* ((keys (org-cite-list-keys info))
  766. (sorted-keys (if (functionp predicate)
  767. (sort keys predicate)
  768. keys))
  769. (position (seq-position sorted-keys key #'string-equal)))
  770. (and (integerp position)
  771. (1+ position))))
  772. (defun org-cite-inside-footnote-p (citation &optional strict)
  773. "Non-nil when CITATION object is contained within a footnote.
  774. When optional argument STRICT is non-nil, return t only if CITATION represents
  775. the sole contents of the footnote, e.g., after calling `org-cite-wrap-citation'.
  776. When non-nil, the return value if the footnote container."
  777. (let ((footnote
  778. (org-element-lineage citation
  779. '(footnote-definition footnote-reference))))
  780. (and footnote
  781. (or (not strict)
  782. (equal (org-element-contents (org-element-property :parent citation))
  783. (list citation)))
  784. ;; Return value.
  785. footnote)))
  786. (defun org-cite-wrap-citation (citation info)
  787. "Wrap an anonymous inline footnote around CITATION object in the parse tree.
  788. INFO is the export state, as a property list.
  789. White space before the citation, if any, are removed. The parse tree is
  790. modified by side-effect.
  791. Return newly created footnote object."
  792. (let ((footnote
  793. (list 'footnote-reference
  794. (list :label nil
  795. :type 'inline
  796. :contents-begin (org-element-property :begin citation)
  797. :contents-end (org-element-property :end citation)
  798. :post-blank (org-element-property :post-blank citation)))))
  799. ;; Remove any white space before citation.
  800. (org-cite--set-previous-post-blank citation 0 info)
  801. ;; Footnote swallows citation.
  802. (org-element-insert-before footnote citation)
  803. (org-element-adopt-elements footnote
  804. (org-element-extract-element citation))))
  805. (defun org-cite-adjust-note (citation info &optional rule punct)
  806. "Adjust note number location for CITATION object, and punctuation around it.
  807. INFO is the export state, as a property list.
  808. Optional argument RULE is the punctuation rule used, as a triplet. When nil,
  809. rule is determined according to `org-cite-note-rules', which see.
  810. Optional argument PUNCT is a list of punctuation marks to be considered.
  811. When nil, it defaults to `org-cite-punctuation-marks'.
  812. Parse tree is modified by side-effect.
  813. Note: when calling both `org-cite-adjust-note' and `org-cite-wrap-citation' on
  814. the same object, call `org-cite-adjust-note' first."
  815. (when org-cite-adjust-note-numbers
  816. (pcase-let* ((rule (or rule (org-cite--get-note-rule info)))
  817. (punct-re (regexp-opt (or punct org-cite-punctuation-marks)))
  818. ;; with Emacs <27.1. Argument of `regexp' form (PUNCT-RE this case)
  819. ;; must be a string literal.
  820. (previous-punct-re
  821. (rx-to-string `(seq (opt (group (regexp ,(rx (0+ (any blank ?\n))))
  822. (regexp ,punct-re)))
  823. (regexp ,(rx (opt (0+ (any blank ?\n)) (group ?\"))
  824. (opt (group (1+ (any blank ?\n))))
  825. string-end)))
  826. t))
  827. (next-punct-re
  828. (rx-to-string `(seq string-start
  829. (group (0+ (any blank ?\n)) (regexp ,punct-re)))
  830. t))
  831. (next (org-export-get-next-element citation info))
  832. (final-punct
  833. (and (stringp next)
  834. (string-match next-punct-re next)
  835. (match-string 1 next)))
  836. (previous
  837. ;; Find the closest terminal object. Consider
  838. ;; citation, subscript and superscript objects as
  839. ;; terminal.
  840. (org-last
  841. (org-element-map (org-export-get-previous-element citation info)
  842. '(citation code entity export-snippet footnote-reference
  843. line-break latex-fragment link plain-text
  844. radio-target statistics-cookie timestamp
  845. verbatim)
  846. #'identity info nil '(citation subscript superscript))))
  847. (`(,punct ,quote ,spacing)
  848. (and (stringp previous)
  849. (string-match previous-punct-re previous)
  850. (list (match-string 1 previous)
  851. (match-string 2 previous)
  852. (match-string 3 previous)))))
  853. ;; Bail you when there is no quote and either no punctuation, or
  854. ;; punctuation on both sides.
  855. (when (or quote (org-xor punct final-punct))
  856. ;; Phase 1: handle punctuation rule.
  857. (pcase rule
  858. ((guard (not quote)) nil)
  859. ;; Move punctuation inside.
  860. (`(,(or `inside (and `adaptive (guard (not spacing)))) . ,_)
  861. ;; This only makes sense if there is a quotation before the
  862. ;; citation that does not end with some punctuation.
  863. (when (and (not punct) final-punct)
  864. ;; Quote guarantees there is a string object before
  865. ;; citation. Likewise, any final punctuation guarantees
  866. ;; there is a string object following citation.
  867. (let ((new-prev
  868. (replace-regexp-in-string
  869. previous-punct-re
  870. (concat final-punct "\"") previous nil nil 2))
  871. (new-next
  872. (replace-regexp-in-string
  873. ;; Before Emacs-27.1 `literal' `rx' form with a variable
  874. ;; as an argument is not available.
  875. (rx-to-string `(seq string-start ,final-punct) t)
  876. "" next)))
  877. (org-element-set-element previous new-prev)
  878. (org-element-set-element next new-next)
  879. (setq previous new-prev)
  880. (setq next new-next)
  881. (setq punct final-punct)
  882. (setq final-punct nil))))
  883. ;; Move punctuation outside.
  884. (`(,(or `outside (and `adaptive (guard spacing))) . ,_)
  885. ;; This is only meaningful if there is some inner
  886. ;; punctuation and no final punctuation already.
  887. (when (and punct (not final-punct))
  888. ;; Inner punctuation guarantees there is text object
  889. ;; before the citation. However, there is no information
  890. ;; about the object following citation, if any.
  891. ;; Therefore, we handle all the possible cases (string,
  892. ;; other type, or none).
  893. (let ((new-prev
  894. (replace-regexp-in-string
  895. previous-punct-re "" previous nil nil 1))
  896. (new-next (if (stringp next) (concat punct next) punct)))
  897. (org-element-set-element previous new-prev)
  898. (cond
  899. ((stringp next)
  900. (org-element-set-element next new-next))
  901. (next
  902. (org-element-insert-before new-next next))
  903. (t
  904. (org-element-adopt-elements
  905. (org-element-property :parent citation)
  906. new-next)))
  907. (setq previous new-prev)
  908. (setq next new-next)
  909. (setq final-punct punct)
  910. (setq punct nil))))
  911. (_
  912. (error "Invalid punctuation rule: %S" rule))))
  913. ;; Phase 2: move citation to its appropriate location.
  914. ;;
  915. ;; First transform relative citation location into a definitive
  916. ;; location, according to the surrounding punctuation.
  917. (pcase rule
  918. (`(,punctuation same ,order)
  919. (setf rule
  920. (list punctuation
  921. (cond
  922. ;; When there is punctuation on both sides, the
  923. ;; citation is necessarily on the outside.
  924. ((and punct final-punct) 'outside)
  925. (punct 'inside)
  926. (final-punct 'outside)
  927. ;; No punctuation: bail out on next step.
  928. (t nil))
  929. order))))
  930. (pcase rule
  931. (`(,_ nil ,_) nil)
  932. (`(,_ inside after)
  933. ;; Citation has to be moved after punct, if there is
  934. ;; a quotation mark, or after final punctuation.
  935. (cond
  936. (quote
  937. (org-cite--insert-at-split previous citation 2 previous-punct-re))
  938. (final-punct
  939. (org-cite--move-punct-before final-punct citation next info))
  940. ;; There is only punct, and we're already after it.
  941. (t nil)))
  942. (`(,_ inside before)
  943. ;; Citation is already behind final-punct, so only consider
  944. ;; other locations.
  945. (when (or punct quote)
  946. (org-cite--insert-at-split previous citation 0 previous-punct-re)))
  947. (`(,_ outside after)
  948. ;; Citation is already after any punct or quote. It can only
  949. ;; move past final punctuation, if there is one.
  950. (when final-punct
  951. (org-cite--move-punct-before final-punct citation next info)))
  952. (`(,_ outside before)
  953. ;; The only non-trivial case is when citation follows punct
  954. ;; without a quote.
  955. (when (and punct (not quote))
  956. (org-cite--insert-at-split previous citation 0 previous-punct-re)))
  957. (_
  958. (error "Invalid punctuation rule: %S" rule))))))
  959. ;;; Tools generating or operating on parsed data
  960. (defun org-cite-parse-elements (s)
  961. "Parse string S as a list of Org elements.
  962. The return value is suitable as a replacement for a
  963. \"print_bibliography\" keyword. As a consequence, the function
  964. raises an error if S contains a headline."
  965. (with-temp-buffer
  966. (insert s)
  967. (pcase (org-element-contents (org-element-parse-buffer))
  968. ('nil nil)
  969. (`(,(and section (guard (eq 'section (org-element-type section)))))
  970. (org-element-contents section))
  971. (_
  972. (error "Headlines cannot replace a keyword")))))
  973. (defun org-cite-parse-objects (s &optional affix)
  974. "Parse string S as a secondary string.
  975. The return value is suitable as a replacement for a citation object.
  976. When optional argument AFFIX is non-nil, restrict the set of allowed object
  977. types to match the contents of a citation affix."
  978. (org-element-parse-secondary-string
  979. s (org-element-restriction (if affix 'citation-reference 'paragraph))))
  980. (defun org-cite-make-paragraph (&rest data)
  981. "Return a paragraph element containing DATA.
  982. DATA are strings, objects or secondary strings."
  983. (apply #'org-element-create 'paragraph nil (apply #'org-cite-concat data)))
  984. (defun org-cite-emphasize (type &rest data)
  985. "Apply emphasis TYPE on DATA.
  986. TYPE is a symbol among `bold', `italic', `strike-through' and `underline'.
  987. DATA are strings, objects or secondary strings. Return an object of type TYPE."
  988. (declare (indent 1))
  989. (unless (memq type '(bold italic strike-through underline))
  990. (error "Wrong emphasis type: %S" type))
  991. (apply #'org-element-create type nil (apply #'org-cite-concat data)))
  992. (defun org-cite-concat (&rest data)
  993. "Concatenate all the DATA arguments and make the result a secondary string.
  994. Each argument may be a string, an object, or a secondary string."
  995. (let ((results nil))
  996. (dolist (datum (reverse data))
  997. (pcase datum
  998. ('nil nil)
  999. ;; Element or object.
  1000. ((pred org-element-type) (push datum results))
  1001. ;; Secondary string.
  1002. ((pred consp) (setq results (append datum results)))
  1003. (_
  1004. (signal
  1005. 'wrong-type-argument
  1006. (list (format "Argument is not a string or a secondary string: %S"
  1007. datum))))))
  1008. results))
  1009. (defun org-cite-mapconcat (function data separator)
  1010. "Apply FUNCTION to each element of DATA, and return a secondary string.
  1011. In between each pair of results, stick SEPARATOR, which may be a string,
  1012. an object, or a secondary string. FUNCTION must be a function of one argument,
  1013. and must return either a string, an object, or a secondary string."
  1014. (and data
  1015. (let ((result (list (funcall function (car data)))))
  1016. (dolist (datum (cdr data))
  1017. (setq result
  1018. (org-cite-concat result separator (funcall function datum))))
  1019. result)))
  1020. ;;; Internal interface with fontification (activate capability)
  1021. (defun org-cite-fontify-default (cite)
  1022. "Fontify CITE with `org-cite' and `org-cite-key' faces.
  1023. CITE is a citation object. The function applies `org-cite' face
  1024. on the whole citation, and `org-cite-key' face on each key."
  1025. (let ((beg (org-element-property :begin cite))
  1026. (end (org-with-point-at (org-element-property :end cite)
  1027. (skip-chars-backward " \t")
  1028. (point))))
  1029. (add-text-properties beg end '(font-lock-multiline t))
  1030. (add-face-text-property beg end 'org-cite)
  1031. (dolist (reference (org-cite-get-references cite))
  1032. (let ((boundaries (org-cite-key-boundaries reference)))
  1033. (add-face-text-property (car boundaries) (cdr boundaries)
  1034. 'org-cite-key)))))
  1035. (defun org-cite-activate (limit)
  1036. "Activate citations from up to LIMIT buffer position.
  1037. Each citation encountered is activated using the appropriate function
  1038. from the processor set in `org-cite-activate-processor'."
  1039. (let* ((name org-cite-activate-processor)
  1040. (activate
  1041. (or (and name
  1042. (org-cite-processor-has-capability-p name 'activate)
  1043. (org-cite-processor-activate (org-cite-get-processor name)))
  1044. #'org-cite-fontify-default)))
  1045. (when (re-search-forward org-element-citation-prefix-re limit t)
  1046. (let ((cite (org-with-point-at (match-beginning 0)
  1047. (org-element-citation-parser))))
  1048. (when cite
  1049. (funcall activate cite)
  1050. ;; Move after cite object and make sure to return
  1051. ;; a non-nil value.
  1052. (goto-char (org-element-property :end cite)))))))
  1053. ;;; Internal interface with Org Export library (export capability)
  1054. (defun org-cite-store-bibliography (info)
  1055. "Store bibliography in the communication channel.
  1056. Bibliography is stored as a list of absolute file names in the `:bibliography'
  1057. property.
  1058. INFO is the communication channel, as a plist. It is modified by side-effect."
  1059. (plist-put info :bibliography (org-cite-list-bibliography-files)))
  1060. (defun org-cite-store-export-processor (info)
  1061. "Store export processor in the `:cite-export' property during export.
  1062. Export processor is stored as a triplet, or nil.
  1063. When non-nil, it is defined as (NAME BIBLIOGRAPHY-STYLE
  1064. CITATION-STYLE) where NAME is a symbol, whereas
  1065. BIBLIOGRAPHY-STYLE and CITATION-STYLE are strings, or nil.
  1066. INFO is the communication channel, as a plist. It is modified by
  1067. side-effect."
  1068. (let* ((err
  1069. (lambda (s)
  1070. (user-error "Invalid cite export processor declaration: %S" s)))
  1071. (processor
  1072. (pcase (plist-get info :cite-export)
  1073. ((or "" `nil) nil)
  1074. ;; Value is a string. It comes from a "cite_export"
  1075. ;; keyword.
  1076. ((and (pred stringp) s)
  1077. (org-cite-read-processor-declaration s))
  1078. ;; Value is an alist. It must come from
  1079. ;; `org-cite-export-processors' variable. Find the most
  1080. ;; appropriate processor according to current export
  1081. ;; back-end.
  1082. ((and (pred consp) alist)
  1083. (let* ((backend (plist-get info :back-end))
  1084. (candidates
  1085. ;; Limit candidates to processors associated to
  1086. ;; back-ends derived from or equal to the current
  1087. ;; one.
  1088. (sort (seq-filter
  1089. (pcase-lambda (`(,key . ,_))
  1090. (org-export-derived-backend-p backend key))
  1091. alist)
  1092. (lambda (a b)
  1093. (org-export-derived-backend-p (car a) (car b))))))
  1094. ;; Select the closest candidate, or fallback to t.
  1095. (pcase (or (car candidates) (assq t alist))
  1096. ('nil nil)
  1097. (`(,_ . ,p)
  1098. ;; Normalize value by turning it into a triplet.
  1099. (pcase p
  1100. (`(,(pred symbolp))
  1101. (append p (list nil nil)))
  1102. (`(,(pred symbolp) ,(pred string-or-null-p))
  1103. (append p (list nil)))
  1104. (`(,(pred symbolp)
  1105. ,(pred string-or-null-p)
  1106. ,(pred string-or-null-p))
  1107. p)
  1108. (_ (funcall err p))))
  1109. (other (funcall err (cdr other))))))
  1110. (other (funcall err other)))))
  1111. (pcase processor
  1112. ('nil nil)
  1113. (`(,name . ,_)
  1114. (cond
  1115. ((not (org-cite-get-processor name))
  1116. (user-error "Unknown processor %S" name))
  1117. ((not (org-cite-processor-has-capability-p name 'export))
  1118. (user-error "Processor %S is unable to handle citation export" name)))))
  1119. (plist-put info :cite-export processor)))
  1120. (defun org-cite-export-citation (citation _ info)
  1121. "Export CITATION object according to INFO property list.
  1122. This function delegates the export of the current citation to the
  1123. selected citation processor."
  1124. (pcase (plist-get info :cite-export)
  1125. ('nil nil)
  1126. (`(,p ,_ ,_)
  1127. (funcall (org-cite-processor-export-citation (org-cite-get-processor p))
  1128. citation
  1129. (org-cite-citation-style citation info)
  1130. (plist-get info :back-end)
  1131. info))
  1132. (other (error "Invalid `:cite-export' value: %S" other))))
  1133. (defun org-cite-export-bibliography (keyword _ info)
  1134. "Return bibliography associated to \"print_bibliography\" KEYWORD.
  1135. BACKEND is the export back-end, as a symbol. INFO is a plist
  1136. used as a communication channel."
  1137. (pcase (plist-get info :cite-export)
  1138. ('nil nil)
  1139. (`(,p ,_ ,_)
  1140. (let ((export-bibilography
  1141. (org-cite-processor-export-bibliography
  1142. (org-cite-get-processor p))))
  1143. (when export-bibilography
  1144. (funcall export-bibilography
  1145. (org-cite-list-keys info)
  1146. (plist-get info :bibliography)
  1147. (org-cite-bibliography-style info)
  1148. (org-cite-bibliography-properties keyword)
  1149. (plist-get info :back-end)
  1150. info))))
  1151. (other (error "Invalid `:cite-export' value: %S" other))))
  1152. (defun org-cite-process-citations (info)
  1153. "Replace all citations in the parse tree.
  1154. INFO is the communication channel, as a plist. Parse tree is modified
  1155. by side-effect."
  1156. (dolist (cite (org-cite-list-citations info))
  1157. (let ((replacement (org-cite-export-citation cite nil info))
  1158. (blanks (or (org-element-property :post-blank cite) 0)))
  1159. (if (null replacement)
  1160. ;; Before removing the citation, transfer its `:post-blank'
  1161. ;; property to the object before, if any.
  1162. (org-cite--set-previous-post-blank cite blanks info)
  1163. ;; Make sure there is a space between a quotation mark and
  1164. ;; a citation. This is particularly important when using
  1165. ;; `adaptive' note rule. See `org-cite-note-rules'.
  1166. (let ((previous (org-export-get-previous-element cite info)))
  1167. (when (and (org-string-nw-p previous)
  1168. (string-suffix-p "\"" previous))
  1169. (org-cite--set-previous-post-blank cite 1 info)))
  1170. (pcase replacement
  1171. ;; String.
  1172. ((pred stringp)
  1173. ;; Handle `:post-blank' before replacing value.
  1174. (let ((output (concat (org-trim replacement)
  1175. (make-string blanks ?\s))))
  1176. (org-element-insert-before (org-export-raw-string output) cite)))
  1177. ;; Single element.
  1178. (`(,(pred symbolp) . ,_)
  1179. (org-cite--set-post-blank replacement blanks)
  1180. (org-element-insert-before replacement cite))
  1181. ;; Secondary string: splice objects at cite's place.
  1182. ;; Transfer `:post-blank' to the last object.
  1183. ((pred consp)
  1184. (let ((last nil))
  1185. (dolist (datum replacement)
  1186. (setq last datum)
  1187. (org-element-insert-before datum cite))
  1188. (org-cite--set-post-blank last blanks)))
  1189. (_
  1190. (error "Invalid return value from citation export processor: %S"
  1191. replacement))))
  1192. (org-element-extract-element cite))))
  1193. (defun org-cite-process-bibliography (info)
  1194. "Replace all \"print_bibliography\" keywords in the parse tree.
  1195. INFO is the communication channel, as a plist. Parse tree is modified
  1196. by side effect."
  1197. (org-element-map (plist-get info :parse-tree) 'keyword
  1198. (lambda (keyword)
  1199. (when (equal "PRINT_BIBLIOGRAPHY" (org-element-property :key keyword))
  1200. (let ((replacement (org-cite-export-bibliography keyword nil info))
  1201. (blanks (or (org-element-property :post-blank keyword) 0)))
  1202. (pcase replacement
  1203. ;; Before removing the citation, transfer its
  1204. ;; `:post-blank' property to the element before, if any.
  1205. ('nil
  1206. (org-cite--set-previous-post-blank keyword blanks info)
  1207. (org-element-extract-element keyword))
  1208. ;; Handle `:post-blank' before replacing keyword with string.
  1209. ((pred stringp)
  1210. (let ((output (concat (org-element-normalize-string replacement)
  1211. (make-string blanks ?\n))))
  1212. (org-element-set-element keyword (org-export-raw-string output))))
  1213. ;; List of elements: splice contents before keyword and
  1214. ;; remove the latter. Transfer `:post-blank' to last
  1215. ;; element.
  1216. ((and `(,(pred listp) . ,_) contents)
  1217. (let ((last nil))
  1218. (dolist (datum contents)
  1219. (setq last datum)
  1220. (org-element-insert-before datum keyword))
  1221. (org-cite--set-post-blank last blanks)
  1222. (org-element-extract-element keyword)))
  1223. ;; Single element: replace the keyword.
  1224. (`(,(pred symbolp) . ,_)
  1225. (org-cite--set-post-blank replacement blanks)
  1226. (org-element-set-element keyword replacement))
  1227. (_
  1228. (error "Invalid return value from citation export processor: %S"
  1229. replacement))))))
  1230. info))
  1231. (defun org-cite-finalize-export (output info)
  1232. "Finalizer for export process.
  1233. OUTPUT is the full output of the export process. INFO is the communication
  1234. channel, as a property list."
  1235. (pcase (plist-get info :cite-export)
  1236. ('nil output)
  1237. (`(,p ,_ ,_)
  1238. (let ((finalizer
  1239. (org-cite-processor-export-finalizer (org-cite-get-processor p))))
  1240. (if (not finalizer)
  1241. output
  1242. (funcall finalizer
  1243. output
  1244. (org-cite-list-keys info)
  1245. (plist-get info :bibliography)
  1246. (org-cite-bibliography-style info)
  1247. (plist-get info :back-end)
  1248. info))))
  1249. (other (error "Invalid `:cite-export' value: %S" other))))
  1250. ;;; Internal interface with `org-open-at-point' (follow capability)
  1251. (defun org-cite-follow (datum arg)
  1252. "Follow citation or citation-reference DATUM.
  1253. Following is done according to the processor set in `org-cite-follow-processor'.
  1254. ARG is the prefix argument received when calling `org-open-at-point', or nil."
  1255. (let ((name org-cite-follow-processor))
  1256. (cond
  1257. ((null name)
  1258. (user-error "No processor set to follow citations"))
  1259. ((not (org-cite-get-processor name))
  1260. (user-error "Unknown processor %S" name))
  1261. ((not (org-cite-processor-has-capability-p name 'follow))
  1262. (user-error "Processor %S cannot follow citations" name))
  1263. (t
  1264. (let ((follow (org-cite-processor-follow (org-cite-get-processor name))))
  1265. (funcall follow datum arg))))))
  1266. ;;; Meta-command for citation insertion (insert capability)
  1267. (defun org-cite--allowed-p (context)
  1268. "Non-nil when a citation can be inserted at point.
  1269. CONTEXT is the element or object at point, as returned by `org-element-context'."
  1270. (let ((type (org-element-type context)))
  1271. (cond
  1272. ;; No citation in attributes, except in parsed ones.
  1273. ;;
  1274. ;; XXX: Inserting citation in a secondary value is not allowed
  1275. ;; yet. Is it useful?
  1276. ((let ((post (org-element-property :post-affiliated context)))
  1277. (and post (< (point) post)))
  1278. (let ((case-fold-search t))
  1279. (looking-back
  1280. (rx-to-string
  1281. `(seq line-start (0+ (any " \t"))
  1282. "#+"
  1283. (or ,@org-element-parsed-keywords)
  1284. ":"
  1285. (0+ nonl))
  1286. t)
  1287. (line-beginning-position))))
  1288. ;; Paragraphs and blank lines at top of document are fine.
  1289. ((memq type '(nil paragraph)))
  1290. ;; So are contents of verse blocks.
  1291. ((eq type 'verse-block)
  1292. (and (>= (point) (org-element-property :contents-begin context))
  1293. (< (point) (org-element-property :contents-end context))))
  1294. ;; In an headline or inlinetask, point must be either on the
  1295. ;; heading itself or on the blank lines below.
  1296. ((memq type '(headline inlinetask))
  1297. (or (not (org-at-heading-p))
  1298. (and (save-excursion
  1299. (beginning-of-line)
  1300. (and (let ((case-fold-search t))
  1301. (not (looking-at-p "\\*+ END[ \t]*$")))
  1302. (let ((case-fold-search nil))
  1303. (looking-at org-complex-heading-regexp))))
  1304. (match-beginning 4)
  1305. (>= (point) (match-beginning 4))
  1306. (or (not (match-beginning 5))
  1307. (< (point) (match-beginning 5))))))
  1308. ;; White spaces after an object or blank lines after an element
  1309. ;; are OK.
  1310. ((>= (point)
  1311. (save-excursion (goto-char (org-element-property :end context))
  1312. (skip-chars-backward " \r\t\n")
  1313. (if (eq (org-element-class context) 'object) (point)
  1314. (line-beginning-position 2)))))
  1315. ;; At the beginning of a footnote definition, right after the
  1316. ;; label, is OK.
  1317. ((eq type 'footnote-definition) (looking-at (rx space)))
  1318. ;; At the start of a list item is fine, as long as the bullet is
  1319. ;; unaffected.
  1320. ((eq type 'item)
  1321. (> (point) (+ (org-element-property :begin context)
  1322. (current-indentation)
  1323. (if (org-element-property :checkbox context)
  1324. 5 1))))
  1325. ;; Other elements are invalid.
  1326. ((eq (org-element-class context) 'element) nil)
  1327. ;; Just before object is fine.
  1328. ((= (point) (org-element-property :begin context)))
  1329. ;; Within recursive object too, but not in a link.
  1330. ((eq type 'link) nil)
  1331. ((eq type 'table-cell)
  1332. ;; :contents-begin is not reliable on empty cells, so special
  1333. ;; case it.
  1334. (<= (save-excursion (skip-chars-backward " \t") (point))
  1335. (org-element-property :contents-end context)))
  1336. ((let ((cbeg (org-element-property :contents-begin context))
  1337. (cend (org-element-property :contents-end context)))
  1338. (and cbeg (>= (point) cbeg) (<= (point) cend)))))))
  1339. (defun org-cite--insert-string-before (string reference)
  1340. "Insert STRING before citation REFERENCE object."
  1341. (org-with-point-at (org-element-property :begin reference)
  1342. (insert string ";")))
  1343. (defun org-cite--insert-string-after (string reference)
  1344. "Insert STRING after citation REFERENCE object."
  1345. (org-with-point-at (org-element-property :end reference)
  1346. ;; Make sure to move forward when we're inserting at point, so the
  1347. ;; insertion can happen multiple times.
  1348. (if (char-equal ?\; (char-before))
  1349. (insert-before-markers string ";")
  1350. (insert-before-markers ";" string))))
  1351. (defun org-cite--keys-to-citation (keys)
  1352. "Build a citation object from a list of citation KEYS.
  1353. Citation keys are strings without the leading \"@\"."
  1354. (apply #'org-element-create
  1355. 'citation
  1356. nil
  1357. (mapcar (lambda (k)
  1358. (org-element-create 'citation-reference (list :key k)))
  1359. keys)))
  1360. (defun org-cite-make-insert-processor (select-key select-style)
  1361. "Build a function appropriate as an insert processor.
  1362. SELECT-KEY is a function called with one argument. When it is
  1363. nil, the function should return a citation key as a string, or
  1364. nil. Otherwise, the function should return a list of such keys,
  1365. or nil. The keys should not have any leading \"@\" character.
  1366. SELECT-STYLE is a function called with one argument, the citation
  1367. object being edited or constructed so far. It should return
  1368. a style string, or nil.
  1369. The return value is a function of two arguments: CONTEXT and ARG.
  1370. CONTEXT is either a citation reference, a citation object, or
  1371. nil. ARG is a prefix argument.
  1372. The generated function inserts or edits a citation at point.
  1373. More specifically,
  1374. On a citation reference:
  1375. - on the prefix or right before th \"@\" character, insert
  1376. a new reference before the current one,
  1377. - on the suffix, insert it after the reference,
  1378. - otherwise, update the cite key, preserving both affixes.
  1379. When ARG is non-nil, remove the reference, possibly removing
  1380. the whole citation if it contains a single reference.
  1381. On a citation object:
  1382. - on the style part, offer to update it,
  1383. - on the global prefix, add a new reference before the first
  1384. one,
  1385. - on the global suffix, add a new reference after the last
  1386. one.
  1387. Elsewhere, insert a citation at point. When ARG is non-nil,
  1388. offer to complete style in addition to references."
  1389. (unless (and (functionp select-key) (functionp select-style))
  1390. (error "Wrong argument type(s)"))
  1391. (lambda (context arg)
  1392. (pcase (org-element-type context)
  1393. ;; When on a citation, check point is not on the blanks after it.
  1394. ;; Otherwise, consider we're after it.
  1395. ((and 'citation
  1396. (guard
  1397. (let ((boundaries (org-cite-boundaries context)))
  1398. (and (< (point) (cdr boundaries))
  1399. (> (point) (car boundaries))))))
  1400. ;; When ARG is non-nil, delete the whole citation. Otherwise,
  1401. ;; action depends on the point.
  1402. (if arg
  1403. (org-cite-delete-citation context)
  1404. (let* ((begin (org-element-property :begin context))
  1405. (style-end (1- (org-with-point-at begin (search-forward ":")))))
  1406. (if (>= style-end (point))
  1407. ;; On style part, edit the style.
  1408. (let ((style-start (+ 5 begin))
  1409. (style (funcall select-style)))
  1410. (unless style (user-error "Aborted"))
  1411. (org-with-point-at style-start
  1412. (delete-region style-start style-end)
  1413. (when (org-string-nw-p style) (insert "/" style))))
  1414. ;; On an affix, insert a new reference before or after
  1415. ;; point.
  1416. (let* ((references (org-cite-get-references context))
  1417. (key (concat "@" (funcall select-key nil))))
  1418. (if (< (point) (org-element-property :contents-begin context))
  1419. (org-cite--insert-string-before key (car references))
  1420. (org-cite--insert-string-after key (org-last references))))))))
  1421. ;; On a citation reference. If ARG is not nil, remove the
  1422. ;; reference. Otherwise, action depends on the point.
  1423. ((and 'citation-reference (guard arg)) (org-cite-delete-citation context))
  1424. ('citation-reference
  1425. (pcase-let* ((`(,start . ,end) (org-cite-key-boundaries context))
  1426. (key (concat "@"
  1427. (or (funcall select-key nil)
  1428. (user-error "Aborted")))))
  1429. ;; Right before the "@" character, do not replace the reference
  1430. ;; at point, but insert a new one before it. It makes adding
  1431. ;; a new reference at the beginning easier in the following
  1432. ;; case: [cite:@key].
  1433. (cond
  1434. ((>= start (point)) (org-cite--insert-string-before key context))
  1435. ((<= end (point)) (org-cite--insert-string-after key context))
  1436. (t
  1437. (org-with-point-at start
  1438. (delete-region start end)
  1439. (insert key))))))
  1440. (_
  1441. (let ((keys (funcall select-key t)))
  1442. (unless keys (user-error "Aborted"))
  1443. (insert
  1444. (format "[cite%s:%s]"
  1445. (if arg
  1446. (let ((style (funcall select-style
  1447. (org-cite--keys-to-citation keys))))
  1448. (if (org-string-nw-p style)
  1449. (concat "/" style)
  1450. ""))
  1451. "")
  1452. (mapconcat (lambda (k) (concat "@" k)) keys "; "))))))))
  1453. ;;;###autoload
  1454. (defun org-cite-insert (arg)
  1455. "Insert a citation at point.
  1456. Insertion is done according to the processor set in `org-cite-insert-processor'.
  1457. ARG is the prefix argument received when calling interactively the function."
  1458. (interactive "P")
  1459. (let ((name org-cite-insert-processor))
  1460. (cond
  1461. ((null name)
  1462. (user-error "No processor set to insert citations"))
  1463. ((not (org-cite-get-processor name))
  1464. (user-error "Unknown processor %S" name))
  1465. ((not (org-cite-processor-has-capability-p name 'insert))
  1466. (user-error "Processor %S cannot insert citations" name))
  1467. (t
  1468. (let ((context (org-element-context))
  1469. (insert (org-cite-processor-insert (org-cite-get-processor name))))
  1470. (cond
  1471. ((memq (org-element-type context) '(citation citation-reference))
  1472. (funcall insert context arg))
  1473. ((org-cite--allowed-p context)
  1474. (funcall insert nil arg))
  1475. (t
  1476. (user-error "Cannot insert a citation here"))))))))
  1477. (provide 'oc)
  1478. ;;; oc.el ends here