oc-natbib.el 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; oc-natbib.el --- Citation processor using natbib LaTeX package -*- 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 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. :safe t)
  60. ;;; Internal functions
  61. (defun org-cite-natbib--style-to-command (style)
  62. "Return command name to use according to STYLE pair."
  63. (pcase style
  64. ;; "author" style.
  65. (`(,(or "author" "a") . ,variant)
  66. (pcase variant
  67. ((or "caps" "c") "\\Citeauthor")
  68. ((or "full" "f") "\\citeauthor*")
  69. (_ "\\citeauthor")))
  70. ;; "noauthor" style.
  71. (`(,(or "noauthor" "na") . ,variant)
  72. (pcase variant
  73. ((or "bare" "b") "\\citeyear")
  74. (_ "\\citeyearpar")))
  75. ;; "nocite" style.
  76. (`(,(or "nocite" "n") . ,_) "\\nocite")
  77. ;; "text" style.
  78. (`(,(or "text" "t") . ,variant)
  79. (pcase variant
  80. ((or "bare" "b") "\\citealt")
  81. ((or "caps" "c") "\\Citet")
  82. ((or "full" "f") "\\citet*")
  83. ((or "bare-caps" "bc") "\\Citealt")
  84. ((or "bare-full" "bf") "\\citealt*")
  85. ((or "caps-full" "cf") "\\Citet*")
  86. ((or "bare-caps-full" "bcf") "\\Citealt*")
  87. (_ "\\citet")))
  88. ;; Default ("nil") style.
  89. (`(,_ . ,variant)
  90. (pcase variant
  91. ((or "bare" "b") "\\citealp")
  92. ((or "caps" "c") "\\Citep")
  93. ((or "full" "f") "\\citep*")
  94. ((or "bare-caps" "bc") "\\Citealp")
  95. ((or "bare-full" "bf") "\\citealp*")
  96. ((or "caps-full" "cf") "\\Citep*")
  97. ((or "bare-caps-full" "bcf") "\\Citealp*")
  98. (_ "\\citep")))
  99. ;; This should not happen.
  100. (_ (error "Invalid style: %S" style))))
  101. (defun org-cite-natbib--build-optional-arguments (citation info)
  102. "Build optional arguments for citation command.
  103. CITATION is the citation object. INFO is the export state, as a property list."
  104. (let* ((origin (pcase (org-cite-get-references citation)
  105. (`(,reference) reference)
  106. (_ citation)))
  107. (suffix (org-element-property :suffix origin))
  108. (prefix (org-element-property :prefix origin)))
  109. (concat (and prefix (format "[%s]" (org-trim (org-export-data prefix info))))
  110. (cond
  111. (suffix (format "[%s]" (org-trim (org-export-data suffix info))))
  112. (prefix "[]")
  113. (t nil)))))
  114. (defun org-cite-natbib--build-arguments (citation)
  115. "Build arguments for citation command for CITATION object."
  116. (format "{%s}"
  117. (mapconcat #'identity
  118. (org-cite-get-references citation t)
  119. ",")))
  120. ;;; Export capability
  121. (defun org-cite-natbib-export-bibliography (_keys files style &rest _)
  122. "Print references from bibliography FILES.
  123. FILES is a list of absolute file names. STYLE is the bibliography style, as
  124. a string or nil."
  125. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  126. (format "\\bibliography{%s}"
  127. (mapconcat #'file-name-sans-extension
  128. files
  129. ","))))
  130. (defun org-cite-natbib-export-citation (citation style _ info)
  131. "Export CITATION object.
  132. STYLE is the citation style, as a pair of strings or nil. INFO is the export
  133. state, as a property list."
  134. (concat (org-cite-natbib--style-to-command style)
  135. (org-cite-natbib--build-optional-arguments citation info)
  136. (org-cite-natbib--build-arguments citation)))
  137. (defun org-cite-natbib-use-package (output &rest _)
  138. "Ensure output requires \"natbib\" package.
  139. OUTPUT is the final output of the export process."
  140. (with-temp-buffer
  141. (save-excursion (insert output))
  142. (when (search-forward "\\begin{document}" nil t)
  143. ;; Ensure there is a \usepackage{natbib} somewhere or add one.
  144. (goto-char (match-beginning 0))
  145. (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{natbib}")))
  146. (unless (re-search-backward re nil t)
  147. (insert
  148. (format "\\usepackage%s{natbib}\n"
  149. (if (null org-cite-natbib-options)
  150. ""
  151. (format "[%s]"
  152. (mapconcat #'symbol-name
  153. org-cite-natbib-options
  154. ","))))))))
  155. (buffer-string)))
  156. ;;; Register `natbib' processor
  157. (org-cite-register-processor 'natbib
  158. :export-bibliography #'org-cite-natbib-export-bibliography
  159. :export-citation #'org-cite-natbib-export-citation
  160. :export-finalizer #'org-cite-natbib-use-package
  161. :cite-styles
  162. '((("author" "a") ("caps" "a") ("full" "f"))
  163. (("noauthor" "na") ("bare" "b"))
  164. (("text" "t")
  165. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  166. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))
  167. (("nil")
  168. ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc")
  169. ("bare-full" "bf") ("caps-full" "cf") ("bare-caps-full" "bcf"))))
  170. (provide 'oc-natbib)
  171. ;;; oc-natbib.el ends here