oc-natbib.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; oc-natbib.el --- Citation processor using natbib LaTeX package -*- 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 registers the `natbib' citation processor, which provides the
  17. ;; "export" capability for citations.
  18. ;; The processor relies on "natbib" LaTeX package. As such it ensures that the
  19. ;; package is properly required in the document's preamble. More accurately, it
  20. ;; will use any "\\usepackage{natbib}" command already present in the document
  21. ;; (e.g., through `org-latex-packages-alist'), or insert one using options
  22. ;; defined in `org-cite-natbib-options'.
  23. ;; It supports the following citation styles:
  24. ;;
  25. ;; - author (a), including caps (c), and full (f) variants,
  26. ;; - noauthor (na), including bare (b) variant,
  27. ;; - text (t), including bare (b), caps (c), full (f), bare-caps (bc),
  28. ;; bare-full (bf), caps-full (cf), and bare-caps-full (bcf) variants,
  29. ;; - default, including bare (b), caps (c), full (f), bare-caps (bc),
  30. ;; bare-full (bf), caps-full (cf), and bare-caps-full (bcf) variants.
  31. ;; Bibliography accepts any style supported by "natbib" package.
  32. ;;; Code:
  33. (require 'oc)
  34. (declare-function org-element-property "org-element" (property element))
  35. (declare-function org-export-data "org-export" (data info))
  36. ;;; Customization
  37. (defcustom org-cite-natbib-options nil
  38. "List of options added to \"natbib\" package.
  39. If \"natbib\" package is already required in the document, e.g., through
  40. `org-latex-packages-alist' variable, these options are ignored."
  41. :group 'org-cite
  42. :package-version '(Org . "9.5")
  43. :type
  44. '(set
  45. (const :tag "use round parentheses (default)" round)
  46. (const :tag "use square brackets" square)
  47. (const :tag "use curly braces" curly)
  48. (const :tag "use angle brackets" angle)
  49. (const :tag "separate multiple citations with colons (default)" colon)
  50. (const :tag "separate multiple citations with comas" comma)
  51. (const :tag "generate author-year citations" authoryear)
  52. (const :tag "generate numerical citations" numbers)
  53. (const :tag "generate superscripted numerical citations" super)
  54. (const :tag "order multiple citations according to the list of references" sort)
  55. (const :tag "order as above, but numerical citations are compressed if possible" sort&compress)
  56. (const :tag "display full author list on first citation, abbreviate the others" longnamesfirst)
  57. (const :tag "redefine \\thebibliography to issue \\section* instead of \\chapter*" sectionbib)
  58. (const :tag "keep all the authors' names in a citation on one line" nonamebreak)))
  59. ;;; Internal functions
  60. (defun org-cite-natbib--style-to-command (style)
  61. "Return command name to use according to STYLE pair."
  62. (pcase style
  63. ;; "author" style.
  64. (`(,(or "author" "a") . ,variant)
  65. (pcase variant
  66. ((or "caps" "c") "\\Citeauthor")
  67. ((or "full" "f") "\\citeauthor*")
  68. (_ "\\citeauthor")))
  69. ;; "noauthor" style.
  70. (`(,(or "noauthor" "na") . ,variant)
  71. (pcase variant
  72. ((or "bare" "b") "\\citeyear")
  73. (_ "\\citeyearpar")))
  74. ;; "nocite" style.
  75. (`(,(or "nocite" "n") . ,_) "\\nocite")
  76. ;; "text" style.
  77. (`(,(or "text" "t") . ,variant)
  78. (pcase variant
  79. ((or "bare" "b") "\\citealt")
  80. ((or "caps" "c") "\\Citet")
  81. ((or "full" "f") "\\citet*")
  82. ((or "bare-caps" "bc") "\\Citealt")
  83. ((or "bare-full" "bf") "\\citealt*")
  84. ((or "caps-full" "cf") "\\Citet*")
  85. ((or "bare-caps-full" "bcf") "\\Citealt*")
  86. (_ "\\citet")))
  87. ;; Default ("nil") style.
  88. (`(,_ . ,variant)
  89. (pcase variant
  90. ((or "bare" "b") "\\citealp")
  91. ((or "caps" "c") "\\Citep")
  92. ((or "full" "f") "\\citep*")
  93. ((or "bare-caps" "bc") "\\Citealp")
  94. ((or "bare-full" "bf") "\\citealp*")
  95. ((or "caps-full" "cf") "\\Citep*")
  96. ((or "bare-caps-full" "bcf") "\\Citealp*")
  97. (_ "\\citep")))
  98. ;; This should not happen.
  99. (_ (error "Invalid style: %S" style))))
  100. (defun org-cite-natbib--build-optional-arguments (citation info)
  101. "Build optional arguments for citation command.
  102. CITATION is the citation object. INFO is the export state, as a property list."
  103. (pcase-let ((`(,prefix . ,suffix) (org-cite-main-affixes citation)))
  104. (concat (and prefix (format "[%s]" (org-trim (org-export-data prefix info))))
  105. (cond
  106. (suffix (format "[%s]" (org-trim (org-export-data suffix info))))
  107. (prefix "[]")
  108. (t nil)))))
  109. (defun org-cite-natbib--build-arguments (citation)
  110. "Build arguments for citation command for CITATION object."
  111. (format "{%s}"
  112. (mapconcat #'identity
  113. (org-cite-get-references citation t)
  114. ",")))
  115. ;;; Export capability
  116. (defun org-cite-natbib-export-bibliography (_keys files style &rest _)
  117. "Print references from bibliography FILES.
  118. FILES is a list of absolute file names. STYLE is the bibliography style, as
  119. a string or nil."
  120. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  121. (format "\\bibliography{%s}"
  122. (mapconcat #'file-name-sans-extension
  123. files
  124. ","))))
  125. (defun org-cite-natbib-export-citation (citation style _ info)
  126. "Export CITATION object.
  127. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  128. state, as a property list."
  129. (concat (org-cite-natbib--style-to-command style)
  130. (org-cite-natbib--build-optional-arguments citation info)
  131. (org-cite-natbib--build-arguments citation)))
  132. (defun org-cite-natbib-use-package (output &rest _)
  133. "Ensure output requires \"natbib\" package.
  134. OUTPUT is the final output of the export process."
  135. (with-temp-buffer
  136. (save-excursion (insert output))
  137. (when (search-forward "\\begin{document}" nil t)
  138. ;; Ensure there is a \usepackage{natbib} somewhere or add one.
  139. (goto-char (match-beginning 0))
  140. (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{natbib}")))
  141. (unless (re-search-backward re nil t)
  142. (insert
  143. (format "\\usepackage%s{natbib}\n"
  144. (if (null org-cite-natbib-options)
  145. ""
  146. (format "[%s]"
  147. (mapconcat #'symbol-name
  148. org-cite-natbib-options
  149. ","))))))))
  150. (buffer-string)))
  151. ;;; Register `natbib' processor
  152. (org-cite-register-processor 'natbib
  153. :export-bibliography #'org-cite-natbib-export-bibliography
  154. :export-citation #'org-cite-natbib-export-citation
  155. :export-finalizer #'org-cite-natbib-use-package
  156. :cite-styles
  157. '((("author" "a") ("caps" "a") ("full" "f"))
  158. (("noauthor" "na") ("bare" "b"))
  159. (("text" "t")
  160. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  161. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))
  162. (("nil")
  163. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  164. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))))
  165. (provide 'oc-natbib)
  166. ;;; oc-natbib.el ends here