oc-natbib.el 7.9 KB

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